Getting te ID of a Selected Item in a dropdownlist.

Discussion in 'ASP .Net' started by Rob Venable, Jan 20, 2004.

  1. Rob Venable

    Rob Venable Guest

    Hi all,
    Can anyone tell me how to retrieve the ID of a selected item from a
    DropDownList. My query brings back the ID and value and I cycle through
    the .Read Method to add items to the DropDownList.
    e.g.
    While dr.Read
    DDL.Items.Add(dr("value"))
    End While

    I can do it if I use this method:
    DDL.DataSource = dr
    DDL.DataTextField = "value"
    DDL.DataValueField = "id"
    DDL.DataBind()

    and then refer the the DataValueField to get the ID.

    This would be the easy way but I have to do it by adding items to the
    DDL.
    Is there a property I can set to make it equal to the ID and then be
    able to retrieve it?

    Thanks

    Rob


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Rob Venable, Jan 20, 2004
    #1
    1. Advertising

  2. Hey Rob,

    Is it adding the list items to the ddl that is giving you trouble? If so,
    here's a way to add them via the reader and display the ID when the ddl is
    changed.

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    If Not IsPostBack Then
    Dim myCMD As SqlCommand = _
    New SqlCommand _
    ("SELECT CategoryID, CategoryName FROM Categories", _
    SqlConnection1)
    SqlConnection1.Open()
    Dim dr As SqlDataReader = myCMD.ExecuteReader()
    Dim lstItem As ListItem
    While dr.Read
    lstItem = New ListItem
    lstItem.Text = dr.Item("CategoryName")
    lstItem.Value = dr.Item("CategoryID")
    DropDownList1.Items.Add(lstItem)
    End While
    dr.Close()
    SqlConnection1.Close()
    End If
    End Sub
    Private Sub DropDownList1_SelectedIndexChanged _
    (ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles DropDownList1.SelectedIndexChanged
    Label1.Text = DropDownList1.SelectedItem.Value.ToString
    End Sub

    <P>
    <asp:DropDownList id="DropDownList1" runat="server"
    AutoPostBack="True"></asp:DropDownList></P>
    <P>
    <asp:Label id="Label1" runat="server">Label</asp:Label></P>


    Ken
    MVP [ASP.NET]

    "Rob Venable" <> wrote in message
    news:%...
    >
    > Hi all,
    > Can anyone tell me how to retrieve the ID of a selected item from a
    > DropDownList. My query brings back the ID and value and I cycle through
    > the .Read Method to add items to the DropDownList.
    > e.g.
    > While dr.Read
    > DDL.Items.Add(dr("value"))
    > End While
    >
    > I can do it if I use this method:
    > DDL.DataSource = dr
    > DDL.DataTextField = "value"
    > DDL.DataValueField = "id"
    > DDL.DataBind()
    >
    > and then refer the the DataValueField to get the ID.
    >
    > This would be the easy way but I have to do it by adding items to the
    > DDL.
    > Is there a property I can set to make it equal to the ID and then be
    > able to retrieve it?
    >
    > Thanks
    >
    > Rob
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Ken Cox [Microsoft MVP], Jan 20, 2004
    #2
    1. Advertising

  3. Rob Venable

    Rob Venable Guest

    Thanks Ken,
    That works fine but I still have one little problem. I need to add a
    blank entry in my dropdownlist because this select box is not a
    mandatory field on my form.
    If I instantiate the listitem inside my while loop, how can I add a
    blank line at the top?

    Thanks for your help.

    Rob

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Rob Venable, Jan 21, 2004
    #3
  4. Rob,

    This is the code you need.

    DropDownlist.Items.Insert(0,new ListItem(" "," "));

    This will add a blank item to the first position of the drop down list.

    Ragards,

    Marshal Antony

    http://dotnetmarshal.com




    "Rob Venable" <> wrote in message
    news:%...
    >
    > Thanks Ken,
    > That works fine but I still have one little problem. I need to add a
    > blank entry in my dropdownlist because this select box is not a
    > mandatory field on my form.
    > If I instantiate the listitem inside my while loop, how can I add a
    > blank line at the top?
    >
    > Thanks for your help.
    >
    > Rob
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Marshal Antony, Jan 21, 2004
    #4
  5. Rob Venable

    Rob Venable Guest

    Thanks Marshal,

    That's exactly what I needed.

    Regards,
    Rob


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Rob Venable, Jan 21, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pTsy
    Replies:
    1
    Views:
    469
    Shiva
    Nov 17, 2004
  2. Iain
    Replies:
    3
    Views:
    969
  3. Nathan Sokalski
    Replies:
    0
    Views:
    651
    Nathan Sokalski
    Oct 5, 2008
  4. mldardy
    Replies:
    0
    Views:
    1,001
    mldardy
    Sep 28, 2010
  5. Nathan Sokalski

    Selected ListItem in DropDownList does not appear selected

    Nathan Sokalski, Oct 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    318
    Nathan Sokalski
    Oct 5, 2008
Loading...

Share This Page