ASP.NET DropDownList not retaining selected item on postback.
The page lifecycle does the following (plus other steps irrelevant to your question):
Otherwise, you have to populate the list manually in the
Edit:
The
The unique values are used for selecting the right item on the server side.The page lifecycle does the following (plus other steps irrelevant to your question):
OnInit
- Populate controls from ViewState (during postback)
- Set the selected values (during postback)
Page_Load
if (!IsPostback) { // Populate }
Otherwise, you have to populate the list manually in the
OnInit
event on every page request. Page_Load
is too late in the lifecycle, so the selected item is lost.Edit:
The
DropDownList
must also have valid values set (separate from the text displayed in the browser). This is done through the DataValueField
property. Each value must be unique, otherwise only the first
duplicate item will ever be selected. If you look at the HTML source in
your browser, you should have:<select>
<option value="unique_value1">Displayed text1</option>
<option value="unique_value2">Displayed text2</option>
</select>
source: http://stackoverflow.com/questions/4189158/asp-net-dropdownlist-not-retaining-selected-item-on-postback
No comments:
Post a Comment