I am trying to bind a List of string to a Dropdown. On frontend, I am able to see the list of dropdown correctly but when I select a value and click on search, it throws a null reference exception because it seems like it is read only. This is what I tried:
<asp:DropDownList runat="server" ID="ddl" AppendDataBoundItems="True">
<asp:ListItem Value="">Please Select</asp:ListItem></asp:DropDownList>
Code behind:
List<string> items = helper.GetData(); //A method that simply returns a list of strings.
ddl.DataSource = items;
ddl.DataBind();
protected void searchClick(object sender, EventArgs e){
/*This is null and when I inspect this, I don't see any value matching
the string selected in dropdown.*/
var selectedOption = ddl.SelectedItem.Text;
}
I tried every possible solution online. I even tried converting it to a dictionary just like it has been given here. I also tried converting it into an object assigning it a title and an ID property as given here
Thank you.