DropDownList in ASP.NET does not retain control state

I’m not a huge fan of ViewState because I’m obsessed when it comes to page speed and ViewState just seems to cluster my webpage with sometimes unwanted info. So I tend to switch viewstate off and guess what, dropdownlist does not retain the selection the user has made before postback!

This, to me, is a serious design flaw within the Microsoft .Net framework because a DropDownList should retain its control state just like a checkbox and not rely on viewstate information being available. It happens that textboxes are able to save their control state even with ViewState off but not DropDownList. Why is that, I wonder?

Anyway, the workaround for this problem if you have turned off ViewState in your application is to request the selection the user has made from the dropdownlist through the request.form variables by passing in the ID of the select box as follows:

public static string GetSelectedValueFromDropDown(System.Web.UI.WebControls.DropDownList listBox)
 return HttpContext.Current.Request[listBox.UniqueID];

Hopefully Microsoft will fix that in a future release 🙂

comments powered by Disqus