Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using DropDownList in Datalist for multiple items.

Status
Not open for further replies.

qwert231

Programmer
Sep 4, 2001
756
0
0
US
I have a datalist that will show a dropdownlist for every item. I want that datalist to reflect a value stored in the database as well as allowing the user to change that value with one of the other items in the list. Any pointers?
 
qwert: See if this helps. In this case I bring together 3 fields in a dropdown list:

Private Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
Dim NV As String = "NV"
Dim dbconnSiteRecs As OleDbConnection = New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=" & Server.MapPath(".\fpdb\Sites.mdb;"))
dbconnSiteRecs.Open()
Dim DBCommand = New OleDbCommand (&quot;SELECT DISTINCT tblZipCodes.City, tblZipCodes.County, WebMasterSites.ZipCode & ' ' & tblZipCodes.County & ' Co., ' & tblZipCodes.City & ', ' & tblZipCodes.State As ZipInfo FROM WebMasterSites LEFT JOIN tblZipCodes ON WebMasterSites.ZipCode = tblZipCodes.Zip_Code WHERE (((WebMasterSites.ZipCode)<>'NV')) ORDER BY tblZipCodes.County, tblZipCodes.City&quot;, dbconnSiteRecs)
Dim reader As OleDbDataReader
ddZip.DataSource = DBCommand.ExecuteReader()
ddZip.DataTextField = &quot;ZipInfo&quot;
ddZip.DataBind()
dbconnSiteRecs.Close()
ddZip.SelectedItem.Text = &quot;Select Zip Code...&quot;
End If
End Sub
 
qwert: My mistake! I thought when I read the thread you were looking for just combining items in a dropdown list. I'll reconsider this and get back with you -- sorry!
 
qwert: Not quite sure what you're trying to do -- although it sounds interesting. Are you starting with a dropdownlist? or a listbox? Pls explain a bit more -- not exactly sure how you want to pull this off.
 
Thanks Is, actually, I am doing an ItemDataBound event that finds my Drop down, sets it's datasource, and also, sets it's Selected.
Here's the code:
Code:
Private Sub dlTest_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlTest.ItemDataBound
  Dim _group As String
  If Not IsDBNull(DataBinder.Eval(e.Item.DataItem, &quot;mGroup&quot;)) Then
   _group = DataBinder.Eval(e.Item.DataItem, &quot;mGroup&quot;)
  Else : _group = &quot;No Group&quot;
  End If
  Dim newDDL As System.Web.UI.WebControls.DropDownList
  newDDL = e.Item.FindControl(&quot;ddlGroups&quot;)
  newDDL.DataSource = Me.DsImgConn1
  newDDL.DataMember = &quot;Groupings&quot;
  newDDL.DataTextField = &quot;GroupName&quot;
  newDDL.DataValueField = &quot;GroupName&quot;
  newDDL.DataBind()
  newDDL.Items.Insert(0, &quot;No Group&quot;)
  Dim _item As ListItem
  For Each _item In newDDL.Items
   If _item.Text = _group Then _item.Selected = True
  Next
 End Sub
 
qwert: This is an excellent question -- and, not being a &quot;guru&quot; (more technical than exp prog) I have to dig a little deeper than most.

What I see you doing here is (1) populating a listbox, and, during the binding of that listbox, you're also populating your dropdown (ddGroups), selecting those field members in the dropdown that match your listbox (setting selected=true).

An expert might drop by and clear this up rather quickly, but I find the problem interesting enough to experiment with it, just need a bit of help here.

my html looks like:

<HTML>
<HEAD>
<TITLE>test list aspx</TITLE>
</HEAD>
<form id=&quot;Form1&quot; runat=&quot;server&quot;>
<asp:ListBox id=&quot;dlTest&quot; runat=&quot;server&quot; OnItemDataBound=&quot;Item_DataBound&quot; Width=&quot;190px&quot; Height=&quot;113px&quot; />
<asp:DropDownList id=&quot;ddGroups&quot; runat=&quot;server&quot; width=&quot;100&quot;/>
<br><asp:Textbox id=&quot;txtA&quot; runat=&quot;server&quot; />
</form>
</BODY>
</HTML>

And also, how exactly do you get your source data from:

newDDL.DataSource = Me.DsImgConn1

Haven't seen that before. The way I see your problem, one populates the list box (using the databound code) and at the same time populates the dropdown, selecting those items which equate to the listbox.

Also, using one table (tblGroups) with &quot;ID&quot; and &quot;mGroups&quot; -- so might be off here.

I hope this is not asking too much, I find this problem very interesting, and I'm sure we can nail it. However, with LV, jfrost and a few other gurus hanging out here, they could probably bring this to a fast resolution --

Hopefully this isn't bothering you too much qwert -- figured since I spoiled your introduction with my misreading the least I can do is give this a shot. Impressive stuff here qwert, good job.

 
Actually, you are a bit mistaken. I am not using a listbox, I am using a DataList, which is similar to a Datagrid. There are multiple items in the DataList representing rows of a datatable. Each item (cell) containts a radiobutton, textbox, and a drop down list.
Code:
<asp:datalist CellPadding=&quot;10&quot; id=dlTest DataMember=&quot;Photos&quot; DataSource=&quot;<%# DsImgConn1 %>&quot; Runat=&quot;server&quot; BorderColor=&quot;black&quot; BorderWidth=&quot;0&quot; CellSpacing=&quot;0&quot; HorizontalAlign=&quot;Center&quot; RepeatDirection=&quot;Horizontal&quot; RepeatColumns=&quot;3&quot;>
     <ItemStyle HorizontalAlign=&quot;Center&quot; VerticalAlign=&quot;Top&quot;></ItemStyle>
     <ItemTemplate>
      <TABLE>
       <TR>
        <TD vAlign=&quot;top&quot;><IMG id=repImg2 src='<%# givePath(Container.DataItem(&quot;ImageName&quot;)) %>'>
        </TD>
        <TD vAlign=&quot;top&quot;><FONT face=&quot;Arial&quot; size=&quot;-2&quot;>PHOTO ID #:
          <asp:Label id=&quot;repLbl&quot;>
           <%# Container.DataItem(&quot;ImageName&quot;) %>
          </asp:Label><BR>
          <HR>
          <asp:CheckBox id=cbHide runat=&quot;server&quot; Text=&quot;Hide&quot; Checked='<%# DataBinder.Eval(Container, &quot;DataItem.Hide&quot;) %>'>
          </asp:CheckBox><BR>
          SORT ORDER<BR>
          <asp:TextBox id=TextBox1 runat=&quot;server&quot; Text='<%# Container.DataItem(&quot;mSort&quot;) %>' Width=&quot;46px&quot;>
          </asp:TextBox><BR>
          GROUP<BR>
          <asp:DropDownList id=&quot;ddlGroups&quot; runat=&quot;server&quot;></asp:DropDownList></FONT></TD>
       </TR>
      </TABLE>
     </ItemTemplate>
    </asp:datalist>

So, By using the afore mentioned code, my drop down list is correctly being populated, with the correct item being selected.
 
qwert: thanks - just used listbox loosely, datalist (asp.net listbox) -- ok; thanks for the code, would like to take a closer look at this -- good problem -- will post back qwert.
 
qwert - I'm having a Monday on a Tuesday I think -- lot was going on -- yes, listbox, datalist, I'm with ya -- at any rate there is enough code here now I can see exactly what it is you are trying to do - see if I can be of help.
 
qwert: After evaluating this a bit more, I think I now have the picture.

You have a datalist, populated from a table with the following fields:
1) image name
2) image path
3) checkbox
4) text value
-----------------------
5) drop down value (cd be 2d table values?)

You're setting the datalist to 3 columns, etc. (I have rendered this much). Now, the trick is to allow the dropdown list to hold various values, and you want the user to be able to select one of those values and save the new value (after having initially shown the value that &quot;was&quot; selected when the page was served - this value would appear in the dropdown list when the page is rendered).

I had trouble (not in VS.NET IDE) getting the OnItemBound dataset(DsImgConn1)to serve as drop down list source. In addition, I threw exceptions on trying to set the Dataset at the level of the DataList html.

It would seem that, if the values used to populate the drop down list are in a 1 to many relationship with the data list item, that a 1 to many table might serve the purpose (keying on an ID field in the main table).

I would think you could then update the tables via the dataset generated using an &quot;..update(DsImgConn1)&quot; type statement.

There might be other approaches - again, this is an excellent problem - could be very useful indeed.

Let me know if my premise is correct or not, if you don't mind, that the values populating the dd list are in fact independent of the various data items.

For example, DataList might look like:

Img: x.gif
Img path: &quot;images/x.gif&quot;
Name: Photo1
Hide: True
ddGroups: Blue

Img: y.gif
Img path: &quot;images/y.gif&quot;
Name: Photo2
Hide: False
ddGroups: Red

Img: z.gif
Img path: &quot;images/z.gif&quot;
Name: Photo3
Hide: False
ddGroups: Yellow

where ddGroups may have, e.g., 26 values (here colors), and the main table populating the data list items only 10 records(as an example).

User can select new color, which then must force an insert (update) event.

I have a few ideas on this (perhaps a diff approach). Your views? Hope I am not being too inquisitive, this is an excellent problem.

 
Thank you. I think you're starting to see it. I have the ddList populating correctly, even selecting the correct value. Now all I need is that when they change the value and hit submit the new value is loaded into the database. As well as if they changed the checkbox and/or the textbox.

I am seeming to have a hard time using the design time created dataAdapter's Update command to update the database with the user's response. I am assuming that while the controls contain the user's data, it is not reflected in the dataset.
 
qwert: Got in late -- there was a thread a few days ago, a person was having trouble updating his odbc file using the update(DataSet) statement...I played around with it for awhile and found a declaration missing that, when put into the code, allowed the update to proceed. Do a quick search over the last 2 or 3 pages max, and see that argument -- sounds like it may be the same as you're discussing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top