"EnableViewState = false" in asp.net DropDownList

If asp.net DropDownList is set to EnableViewState = false, DropDownList.SelectedItem returns null.

Then, what technique can I use to get the SelectedItem of this DropDownList while keeping this EnableViewState = false?

+4
source share
5 answers
Request.Form[yourDropDownList.UniqueID] 

UniqueID returns "ctl00 $ ContentMain $ ddlCountry", where as ClientID returns "ctl00_ContentMain_ddlCountry"

A unique identifier is inserted in the name of the HTML form that is used in the name of the reverse form.

+7
source
 this.Request.Form[this.List.ClientID]; 
+4
source

My guess is that the problem is that you are binding data to a drop-down list manually in the code located behind the file. This can help move the data binding from Page_Load() to Page_Init() .

But, in my opinion, the best solution would be to use an ObjectDataSource to populate the values ​​of your DropDownList. This way you are sure that you are loading values ​​at the right time in the page life cycle.

+4
source
 string selectedValue = Request.Form["MyDropDownList"]; 
+2
source

Here is my solution using VS2010. 1. Set EnableViewState to false. 2. Bind dropdownlistbox to sqldataadapter.

Before: After setting EnableViewState to false, I had to reload the list with every postback. It worked. However, the first time a value was selected from the list, the SelectedIndexChanged event was fired, but selectecitem.Value did not change (it remained at "0"). The second time the value was selected from the list, selectecitem.Value was correct.

+1
source

All Articles