Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  


DropDownList Nested in GridView Loses Value After Postback
A dropdown list created on an.aspx page or designer with static list items that is nested in a Template Column on an ASP.NET GridView in ASP.NET 4.0 will not retain its selected value if the page posts back. It resets to the first item in the list, instead of restoring the last selected item.

I don't know why this is; it seems like a bug. However, I found the following workaround which resolved the issue:
Instead of creating the static drop down list on the aspx page (with asp:ListItems), create a custom dropdown Server Control (a class that inherits from DropDownList). Initialize the ListItems in the class's constructor. Register the server control on your page or in your web.config, and replace the asp:DropDownList with your custom:NewDropDownList.

Now, when the page posts back, the selected value will be retained.

Created By: amos 11/2/2011 11:47:34 AM
Updated: 4/6/2015 3:25:18 AM