Reputation: 2680
When I bind the value of a TextBox in the .aspx page, I do the following inside a ListView:
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
How can I bind the value from a DropDownList the same way?
<asp:DropDownList ID="TestDropDownList" runat="server">
<asp:ListItem Value="Test 1">Test 1</asp:ListItem>
<asp:ListItem Value="Test 2">Test 2</asp:ListItem>
</asp:DropDownList>
Upvotes: 0
Views: 3758
Reputation: 103358
To bind to a DropDownList, you would bind your datasource to the DropDownList control, and specify your Text and Value at that stage like:
cmd SqlCommand = new SqlCommand("Your SQL Command Here", conn);
TestDropDownList.DataSource = cmd.ExecuteReader();
TestDropDownList.DataTextField = "Name";
TestDropDownList.DataValueField = "Value";
TestDropDownList.DataBind();
This is the equivalent of you trying to do something like this:
<asp:DropDownList ID="TestDropDownList" runat="server">
<asp:ListItem Value="<%# Bind("Value") %>"><%# Bind("Name") %></asp:ListItem>
</asp:DropDownList>
Upvotes: 3