I’m slowly going mad over the following problem: In my ASP.NET page (target: .NET 4.0) I have the following declaration of a DropDownList:
<asp:DropDownList ID="cmbRequestState" runat="server" EnableViewState="true">
<asp:ListItem Text="Pending" Value="0"></asp:ListItem>
<asp:ListItem Text="Open" Value="1"></asp:ListItem>
<asp:ListItem Text="Closed" Value="2"></asp:ListItem>
</asp:DropDownList>
In the Page_Load event in code-behind I set the cmbRequestState value to “2” for example – everything’s fine, the value shown in the combo box in the browser is “Closed”, as the respective <option> tag in HTML has the selected="selected" attribute.
Then I select “Open” and perform a postback using a “Save”-button on my page. When I retrieve the SelectedValue for cmbRequestState in the button’s code, it is still “2” even though I’ve selected “1”.
Now I don’t write ASP.NET applications too often, but I do have some knowledge in HTML and PHP programming and from my WinForms and WPF background I’d expect the SelectedValue to contain the value that is currently selected… Also, I do not want to perform a postback every time a user selects a value from the list.
What am I missing here? How would I make sure that when opening the page, the current request state is selected and still get the new selection if the user makes a change?
According to the page lifecycle, the selectedvalue you are assigning in the
Page_Loadevent overwrites the value you are setting in the button’sOnClickevent. You should try to match the page lifecycle when assigning values to your controls.