I am having a problem with an ASP.NET DropDownList
which is populated by an XML file:
rblState.DataSource = dsState;
rblState.DataValueField = "abbreviation";
rblState.DataTextField = "name";
rblState.DataBind();
This works fine and displays all the right data however, the problem occurs when I try and retrieve the selected value from the list after a button has been clicked:
string state = rblState.SelectedItem.Text;
Console.WriteLine(state);
This always outputs only the first value within the list.
Anyone know the solution to this?
You are probably re-binding the DataSource
on PostBack. Instead, do this:
//only bind on the first request
if (!Page.IsPostBack)
{
rblState.DataSource = dsState;
rblState.DataValueField = "abbreviation";
rblState.DataTextField = "name";
rblState.DataBind();
}
Try putting your populating codes in
if (!Page.IsPostBack)
{
//your code here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With