Link to home
Start Free TrialLog in
Avatar of jimwal1940
jimwal1940

asked on

EnableViewState = False causes items not to write to database

I was building an application that queries a database to populate 10 pulldowns.  One issue is that each of these 10 dropdowns has about 5000 drug names in each one.  I was getting a " Maximum Request Length Exceeded" but was adviced to turn off view state on each drop down so I did and it seemed to allow the record to write.  I just found out though that those dropdowns that I made EnableViewState = false aren't writting or being placed into the array variables to be written to the database... When I make EnableViewState = True they are assigned to the variales but since the Event Viewis so long I continue to get the above error...

Thoughts Any one ?????
Avatar of saddy1
saddy1

Instead of using the Dropdowns as controls and accessing their data that way, I would use the more traditional way of checking the POST object, via Request.Form.  So if your Dropdown is called "DD1", you can access what was selected using Request.Form["DD1"], which doesn't require the Viewstate.  Passing thousands of unused dropdown selections is really inefficient anyway, at will probably slow down your site significantly, since the user has to download each entry twice - once in the dropdown itself, and once in the Viewstate.

--saddy1--
Avatar of jimwal1940

ASKER

SO Saddy currently I have the dropdowns that get populated via a database table.... and load the selections into an array such as this ...
values.Add(drp_meds1.SelectedValue);
values.Add(drp_meds2.SelectedValue);
 values.Add(drp_meds3.SelectedValue);
values.Add(drp_meds4.SelectedValue);
values.Add(drp_meds5.SelectedValue);
values.Add(drp_meds6.SelectedValue);
values.Add(drp_meds7.SelectedValue);
values.Add(drp_meds8.SelectedValue);
values.Add(drp_meds9.SelectedValue);
values.Add(drp_meds10.SelectedValue);

What would the code look like in your solution ?
ASKER CERTIFIED SOLUTION
Avatar of saddy1
saddy1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you switch off viewstate, then you must reconstruct the dropdownlist across every postback and obtain the data of the dropdownlist from the RequestForm collection.

The proper way to get this is the following:
// get the name via the UniqueID property, the same would generally apply for controls inside UserControls
// if the controls are inside any control implementing the INamingContainer, then the property will not be accurate in giving the name for retrieval.
string name = ddl.UniqueID;
string value = Request.Form[name] == null ? string.Empty : Request.Form[name];
// if the controls are inside any control implementing the INamingContainer, then the property will not be accurate in giving the name for retrieval. <-- will not apply to controls found imemdiately inside the UserControl