1

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.

Community
  • 1
  • 1
Samyukta
  • 92
  • 1
  • 11

3 Answers3

1

You have bind list in dropdownlist but has not append which value has to be fetched when any value got selected.

If(!isPostBack())
{
ddl.DataSource = items;
ddl.DataTextField = "Field name which hold items(Text to be shown in ddl)";
ddl.DataValueField = "ID of items(Value for items)";
ddl.DataBind();
}

If have only list and have to append in dropdown list then use bleow code.

List<string> items=helper.GetData();
for(var i=0; i < items.Count; i++)
{
    ddl.Items.Add(new ListItem(i, items[i]));
    //ddl.Items.Add(new ListItem(key, source)); For reference only
}

To get value you can use:

ddl.SelectedItem.Value;
ddl.SelectedItem.Text;
Nayan Katkani
  • 806
  • 8
  • 18
  • 1
    And what should the values of `DataTextField` and `DataValueField` be when using a `List`? You can bind them just fine like OP is doing. – VDWWD Nov 02 '16 at 11:01
  • I am no longer getting the null reference after using the second snippet from your code sample, if I do it on the condition that isPostBack is true. But if I do this, then every time my list is repopulated and on clicking search, the selected value always defaults to the first value of the dropdown list. Any suggestions for that? – Samyukta Nov 02 '16 at 15:24
  • @codeet, Sorry i have forgot to make PostBack false, If it will be true then each time your page get refreshed please make it false it will load only first time. I have updated code for that please have a look over it. – Nayan Katkani Nov 03 '16 at 05:15
  • @Nayan, If I make PostBack False, I get the null reference exception again. and keeping PostBack true, the dropdown list gets loaded every time and picks the first value as the selected item. Not sure what to do now. – Samyukta Nov 04 '16 at 19:15
  • Can you please share StackTrace for the error and also code as there is something you are missing or the position you are try to get is not available. – Nayan Katkani Nov 06 '16 at 09:20
0

Having a look at this I notice 2 things:

  1. If the first 4 lines are in your page load make sure that you wrap them in a

    if(!isPostBack) {

    }

If it is not wrapped in the !isPostBack then the value will default to the first value every time a postback occurs as the ddl wil get rebound each time.

  1. As mentioned above you are not specifying the DataTextField or DataValueField. If these arent specified you will get object types in the dropdownlist instead of values.

Also, consider using ddl.SelectedValue rather than SelectedItem.Text as this will give you the unique ID assigned to the DataValueField

ThatChris
  • 752
  • 1
  • 4
  • 18
  • Your answer is not correct. Yes you should wrap the databinding inside `isPostBack` check, but the SelectedValue will still be maintained after PostBack. Only the DropDownList will grow every time since on every load the list gets appended. Second your point 2 is also incorrect, see my comment at @Nayan Katkani answer – VDWWD Nov 02 '16 at 11:06
  • I disagree with you on one of your points. 1. If the OP was running a loop to add the dropdownlist items on every postback I agree, in that case the items will be duplicated. However the OP is resetting the datasource and rebinding the dropdownlist on each postback without the !isPostback, resetting the selected index to 0 2. You are correct here for this specific example, however in a normal use case you would seldom not have a specific text/value pair – ThatChris Nov 02 '16 at 11:20
  • 1
    Op has `AppendDataBoundItems="True"` in the DropDownList. This will cause the ListItems to be appended every time. Otherwise the `SelectedValue` would indeed be lost. – VDWWD Nov 02 '16 at 11:31
-1
 Dictionary<string, string> openWith = 
        new Dictionary<string, string>(); 

insted of list you need to use following dropdown

ddl.DataSource = items;
ddl.DataTextField = "Text Which Show In DropDown";
ddl.DataValueField = "value -When Text Is Select From Dropdown";
ddl.DataBind();