Reputation: 7542
I have a drop down list that pulls data from a database to show some of its options:
<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>
As you can see, I only display the [Name] column:
DataTextField="Name"
But I am also getting the primary key SELECT [PK_Task], [Name] FROM:
DataValueField="PK_Task"
How do I get access to this primary key based on user selection. If a user selects a specific 'name' from the drop down menu, what C# can I use to get the corresponding primary key returned to me so I can save it in a variable?
The closest I have gotten is this, but all it does is return the string "PK_Task":
String taskID = DropDownList3.DataValueField;
Upvotes: 2
Views: 40746
Reputation: 4596
any way you can use either
dropdown3.selectedvalue
or
dropdown3.selecteditem.value
Upvotes: 0
Reputation: 38618
You can use the SelectedValue
property:
// to set
DropDownList3.SelectedValue = "1";
// to read
string value = DropDownList3.SelectedValue;
Upvotes: 5