DropDownList problem - BUG or What?

student2008

New Member
Every time I bind my DropDownList to a database table,<BR>the statement:<BR><BR>DropDownList1.SelectedItem.Value<BR><BR>Always returns the first item. Even if I had a different selection. But when I hard code the listItems, It works well.<BR><BR>Can someone tell me why?<BR>I had the same problem. Point is that if you look at the source code generated, the first item is selected and that is what is sent in the form. A work around is to set up an ASP:dropdownlist<BR>and fill it in code. ie :<BR>...<BR>...<BR>...'get data<BR>'loop thru recordset<BR>WHILE dr.Read()<BR>'add row to ddl<BR>setDDL(ddlCategory,dr.Item("category"),dr.Item("category"))<BR>END WHILE<BR>'set default selection<BR>setDDL(ddlCategory,"Select Category","none")<BR>DataBind()<BR>dr.Close<BR>END SUB<BR><BR>'add item to a ddl<BR>Sub setDDL(ddl,type,value)<BR> Dim MyItem as ListItem = New ListItem()<BR> MyItem.Value = http://aspmessageboard.com/archive/index.php/value<BR> MyItem.Text = type<BR> ddl.Items.Insert(0,MyItem)<BR>End Sub<BR><BR><BR><asp:DropDownList AutoPostBack="False" id="ddlCategory" runat="server" class="formField" /><BR><BR>T&nbsp;<BR>myConnection.Open()<BR>Dim dr As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConne ction)<BR>While dr.Read()<BR> cbo.Items.Add(New ListItem(dr.Item(1), dr.Item(0)))<BR>End While <BR>dr.Close()<BR>
 
Back
Top