I am using ASP.NET 3.5
I have a drop-down list called lstCountry with an item in it like this:
<asp:ListItem Value="United States">Canada</asp:ListItem>
This will display Canada but in code the value will be "United States". How can I retrieve the value "Canada" also in my code?
I have tried all of these and all of them return "United States"
lstCountry.Text
lstCountry.SelectedValue
lstCountry.SelectedItem.Text
My Drop Down list:
<asp:DropDownList ID="lstCountry" runat="server" Width="200px">
<asp:ListItem>Please Select</asp:ListItem>
<asp:ListItem>United States</asp:ListItem>
<asp:ListItem Value="United States">Canada</asp:ListItem>
</asp:DropDownList>
How I read the value in code:
Dim country As String
country = lstCountry.SelectedItem.Text
This question is related to
c#
.net
asp.net
vb.net
drop-down-menu
try
lstCountry.SelectedItem.Text
What about
lstCountry.Items[lstCountry.SelectedIndex].Text;
You can try
lstCountry.SelectedItem.Text
Have a look here, this has a proof-of-concept page and demo you can use to get anything from the drop-down: asp:DropDownList Control Tutorial Page
had the same problem and just solved it, i used string [variable_Name] =dropdownlist1.SelectedItem.Text;
AppendDataBoundItems="true"
needs to be set.
Source: Stackoverflow.com