Search code examples
c#asp.netautopostback

Selected Item not being updated?


I've got the following DropDownList control:

<asp:DropDownList ID="SubjectFilter" runat="server" AutoPostBack="True" onselectedindexchanged="SubjectFilter_SelectedIndexChanged"></asp:DropDownList>

SubjectFilter data:

BookStore b = new BookStore();
b.LoadFromXML(Server.MapPath("list.xml"));

SubjectFilter.DataSource = b.BooksList.Select(x => x.Subject).Distinct().ToArray();
SubjectFilter.DataBind();
SubjectFilter.Items.Insert(0, new ListItem("הכל", "Default"));

Everything loads just fine. However in the SubjectFilter_SelectedIndexChanged method, SubjectFilter.SelectedValue is always Default, even though I'm selecting different options.

What is the problem? Thank you very much.


Solution

  • Make sure that in your Page_Load that you only populate the dropdown when IsPostBack is false.

    For example

     public void Page_Load(...)
     {
          if (!IsPostback())
              UpdateDisplay();
     }