我有一個下拉列表,從數據庫中提取數據,以顯示它的一些選項:的DropDownList,越來越DataValueField返回在C#
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1"
DataTextField="Name" DataValueField="PK_Task" AppendDataBoundItems="true" CssClass="dropDownList">
<asp:ListItem Selected="True">General Support</asp:ListItem>
<asp:ListItem>Vacation</asp:ListItem>
<asp:ListItem>Sick Leave</asp:ListItem>
<asp:ListItem>Something Else</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [PK_Task], [Name] FROM [Task] WHERE ([PointPerson] LIKE '%' + @PointPerson + '%') AND [Status] NOT LIKE 'Done'">
<SelectParameters>
<asp:QueryStringParameter Name="PointPerson" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
正如你所看到的,我只顯示[名稱]列:
DataTextField="Name"
但我也越來越主鍵SELECT [PK_Task],[名] FROM:
DataValueField="PK_Task"
如何獲得根據用戶選擇訪問此主鍵。如果用戶從下拉菜單中選擇了特定的「名稱」,我可以使用什麼C#來獲取相應的主鍵,以便將其保存在變量中?
我得到的最接近的是這一點,但它的作用是返回字符串 「PK_Task」:
String taskID = DropDownList3.DataValueField;
可能重複:http://stackoverflow.com/questions/12640676/asp-net-datavaluefield-in-dropdownlist –