DropDownList holds one more value

2019-08-08 19:02发布

问题:

I have dropdownlist which has items that text and value property of the item are set to primary key. Yes, I show primary key with text property and querying with value property.

I want also to get selected item's other property such as name without querying dataset that binds to dropdownlist or querying database.

How can I do that?

<asp:DropDownList ID="ddl" runat="server">
    <asp:ListItem Text="ItemID" Value="ItemID"></asp:ListItem> // I want get item's name
</asp:DropDownList>

回答1:

DropDownList Aspx Markup :

<asp:DropDownList ID="ddlDropDown" runat="server">
    <asp:ListItem Text="ItemID" Value="ItemID" ThirdValue="ItemName" />
</asp:DropDownList>

Retrieve value like this :

ListItem item = ddlDropDown.Items.FindByValue("ItemID");
string value = item.Attributes["ThirdValue"];


回答2:

If you want to get only one property such as name, you might want to do that

<asp:DropDownList ID="ddl" runat="server">
    <asp:ListItem Text="ItemID" Value="ItemName"></asp:ListItem> 
</asp:DropDownList>

You can query with Text property which is primary key ItemID and get Value property which is ItemName with

SelectedItem.Value