0
votes

I've an array list which has 3 fields(id, description, typeid) and I binded that arraylist to ASP DropDownList. I set description field as DataTextField and another field "Id" DataValueField during bind.

Code:

Dim b As ArrayList
            b = //Assigning ArrayList

            Me.drpDwn.DataSource = b
            Me.drpDwn.DataTextField = "description"
            Me.drpDwn.DataValueField = "id"
            Try : Me.drpDwn.DataBind() : Catch ex As Exception : ErrorEmailAlert.SendErrorViaEmail(Server, Session, ex) : End Try

When the user selects value from the DropDownList, i need typeid value also need set to variable.

Can Someone one please let me know how get value of "typeid" also when i choose particular ID?

EDIT: I need the value of typeid for the selected value in code behind. Not the id(data value) value or description(data field)

2
I'm looking to get the value of typeid for the selected value in code behind. Not the id(data value) value or description(data field) @dotNET - Knowledge2Share

2 Answers

0
votes

You can concat id with typeid and then add it to dropdownlist like this :

class Item
{
    public   int id;
    public string description;
    public int typeid;
}


void FillDropDown()
{
    List<Item> Items = new List<Item>();

    // Fill Items

    DropDownList.Items.Clear();
    Items.ForEach(I => 
    {
        DropDownList.Items.Add(new ListItem(I.description, I.id + ";" + I.typeid));
    });
}
0
votes

The general idea in all binding situations is that you bind DataValueField with the unique identifier (usually the primary key if this data is from a database) of your collection. Once user chooses an item and it is posted back to the server, you can use the primary key value to retrieve the whole record, thereupon getting the value of any other attribute (typeid in your case).