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.DataSourc开发者_运维技巧e = 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.
I'm guessing the above code is from the PageLoad
event. You may want to wrap that in a if(!isPostBack)
block.
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();
}
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..
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.
精彩评论