views:

30

answers:

4

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.

+2  A: 

I'm guessing the above code is from the PageLoad event. You may want to wrap that in a if(!isPostBack) block.

klausbyskov
+1 because in fairness you did beat me to the answer :)
Dead account
+1  A: 

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();
 }
Dead account
This was my problem. I forgot it! Thank you very much (:
iTayb
+1  A: 

When are you binding the drop down? You may any to wrap in an If(page.ispostback ==false) It looks like you may be binding on page load, before you check its value..

Paul
+1  A: 

ViewState is assigned between the Init and Load for an ASP.NET page. Your event handlers occur after load. If you are programmatically setting content in the controls that your user will be using, you want to handle that before ViewState is applied. In other words, move it to Page_Init. Afterwards, ViewState kicks in and you'll see what the user actually selected when your handler executes.

Anthony Pegram