Dropdown list question

Discussion in 'C#' started by popcornmonsters, Dec 4, 2009.

  1. #1
    Hello, I have a dropdown box that pull data from a sql database. My two important values in the table are Peoples names and a ID key. My dropdown box will list all the peoples names. The issue I am having is how do I know what ID the user picks. My website uses the id to display the correct page, ie. I have www.mydomain.com/name.aspx?ID=1. As some of the values in the table have been deleted they are not in order, so there may be ID=1, but ID=2 is deleted. Thanks.
     
    popcornmonsters, Dec 4, 2009 IP
  2. exit7

    exit7 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    List items in ASP.NET have both a text and a value property, you can add an item like this:

    myDropDown.Items.Add(New ListItem("Sue", "1"))
    myDropDown.Items.Add(New ListItem("Bob", "3"))

    Then you all you do is get myDropDown.SelectedItem.Value after the user makes a selection.
     
    exit7, Dec 4, 2009 IP
  3. Harish Singla

    Harish Singla Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    When do adding records to ur drop down list use the following code
    dropdown.datasource = yourdatasourcename;
    dropdown.DataTextField = "name";
    dropdown.DataValueField = "id";
    dropdown.DataBind();

    whenever user click select a name in your dropdown list
    u can easily identify the id by using

    int id = dropdown.selectedItem.value;
     
    Harish Singla, Dec 4, 2009 IP