Dynamic drop down populated with database field

Last post 08-15-2005 1:33 PM by PeterBrunone. 4 replies.

Sort Posts:

  • Dynamic drop down populated with database field

    08-15-2005, 6:52 AM
    • Member
      184 point Member
    • marcuscoker
    • Member since 08-02-2005, 8:26 AM
    • Posts 67

    Hi

    I have a dynamic drop down list on a page. This all works fine using

    Sub assistanceType_SelectedIndexChanged(sender As Object, e As EventArgs)

    response.write("<br>Text Selected: " & assistanceType.SelectedItem.text)
    response.write("<br>Value Selected: " & assistanceType.SelectedItem.Value)
    End Sub

    ....

    Hoevere, I would like to make it so that the inital value selected in the drop down list comes from a value already stored in the database. I have done this for aa textbox by using the code

    <asp:TextBox id="drugName" runat="server" Text='<%# DataSet1.FieldValue("drugName", Container) %>' Width="445px"></asp:TextBox>

    How would I put the <%# DataSet1.FieldValue("drugName", Container) %> into a drop down list to ensure that that value is the initial value selected, whilst still allowing the  user to select any of the other values from the drop down list dynamicly generated by the database?

    Thanks

    Marcus

  • Re: Dynamic drop down populated with database field

    08-15-2005, 7:33 AM
    • Member
      255 point Member
    • dodiggitydag
    • Member since 08-10-2005, 5:33 PM
    • Michigan, USA
    • Posts 51
    From what I can tell, you need to do is to set the selected item on page load.  You can do this:

    <asp:DropDownList id="ddlEdit1" runat="server" DataSource="<%# getBrands() %>" DataValueField="PK_Brand_ID" DataTextField="Brand_Name" SelectedIndex='<%# GetSelIndex(Container.DataItem("Brand_Name")) %>' />

    You can make a public function in the code behind page that will return the zero-based index of the item that should be selected.  In this example the getBrands() returns a DataSet and the GetSelIndex(...)  returns an integer.

    If you don't save the DataTable then you must iterate through the entire ListItemCollection until you find the appropriate item by using the string parameter sent to the function.

        Function GetSelIndex(ByVal BrandName As String) As Integer
            'Loop through each row in the DataSet
            Dim dt As DataTable = getBrands()
            For i As Integer = 0 To dt.Rows.Count - 1
                If BrandName = dt.Rows(i)("Brand_Name") Then Return i
            Next
        End Function

    This is a bad example because the database is queried for the same infomation twice because getBrands() is called twice.  But it does show you how to write your own code that works the same way.

  • Re: Dynamic drop down populated with database field

    08-15-2005, 11:56 AM
    • All-Star
      18,400 point All-Star
    • PeterBrunone
    • Member since 06-19-2002, 5:15 AM
    • I'm standing behind you.
    • Posts 3,683
    • TrustedFriends-MVPs
    I would recommend databinding the list in the more standard ASP.NET fashion (i.e. ddl.Datasource=blah, ddl.Databind()) and then looping through the ListItems collection to get the selected value; it may be a bit quicker than iterating through a DataTable.

    Of course EasyListBox would allow you to set myList.SelectedValue = whatevervalue, but I don't know if you have the permission or motivation to use a third-party control.

    Cheers,
    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: Dynamic drop down populated with database field

    08-15-2005, 1:09 PM
    • Member
      184 point Member
    • marcuscoker
    • Member since 08-02-2005, 8:26 AM
    • Posts 67

    Hi
    Thanks for that, it is unlikely that I would be able to use any thrid party components, but if poss, please could you post the code for databinding as you described in your post.

    Thanks

    Marcus

  • Re: Dynamic drop down populated with database field

    08-15-2005, 1:33 PM
    • All-Star
      18,400 point All-Star
    • PeterBrunone
    • Member since 06-19-2002, 5:15 AM
    • I'm standing behind you.
    • Posts 3,683
    • TrustedFriends-MVPs
    Sure.  This is pretty much standard for all databound controls (at least until they change it in 2.0):

    myList.DataSource = SomeDataTableOrDataSetIfYouLike
    myList.DataBind()

    Dim iRow As Integer = 0

    For iRow = 0 To myList.Items.Count - 1
       If myList.Items(i).Value = "whateverstringvalueyouwanttofind"
          myList.SelectedIndex = iRow
          Exit For
       End If
    Next

    Cheers,
    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
Page 1 of 1 (5 items)