views:

714

answers:

4

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

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

+2  A: 
string selectedValue = Request.Form["MyDropDownList"];
Developer Art
+3  A: 
this.Request.Form[this.List.ClientID];
ChaosPandion
+1  A: 

My guess is that the problem is that you're data-binding the drop down list manually in the code behind file. It might help to move your data binding from Page_Load() to Page_Init().

But in my opinion a better solution would be to use an ObjectDataSource to fill in the values of your DropDownList. That way you're sure that you're loading in the values a the correct time in the page life cycle.

Jan Aagaard
+1  A: 
Request.Form[yourDropDownList.UniqueID] 

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

The UniqueID is inserted to the name of the HTML form which is used in the postback form name.

Jason Jong