DropDownList holds one more value

旧街凉风 提交于 2020-01-06 04:55:06

问题


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


来源:https://stackoverflow.com/questions/27439908/dropdownlist-holds-one-more-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!