DropDownList and the ViewState

You may also like...

14 Responses

  1. Jan Schreuder says:

    Do I REALLY need to kick down that open door

    🙂

  2. Patrick Wellink says:

    Well, was this the first time you used a Dropdownlist that was actually filled with data ? 😉

  3. Erwyn van der Meer says:

    Do you know for sure you want to report this on your blog for the whole wide world to read? 🙂 I know at least two colleagues who wanted to ask you if you had turned off view state. But they didn’t because they thought that was too obvious 🙂

  4. Hehehehe, well, I didn’t think so. Why should it need viewstate? We didn’t have viewstate before .NET and my dropdownlists really worked! So I think it’s pretty stupid it now needs viewstate…

    But maybe I should read the document Ernst posted so proud! 😉

  5. Steph says:

    Hi,

    I am with Dennis 100% there as I am having the exact same problem but I have proof that the implementation of the dropdown is somewhat at fault or at least different from any other controls regarding the handling of its viewstate.

    I have a simple page with one dropdownlist filled in by databind() from a DB, one textbox and one checkbox and a button.

    The test is easy: turn off the viewstate of all the controls, put a break point in the button.click() method in the code behind. Launch your page.

    Now type something in the textbox, check the checkbox, choose an option from the dropdown and click the button.

    What do you see in the debugger for the value of the controls?

    -The text property of the textbox has got the value you typed, despite viewstate false

    -The checkbox.checked property is true, despite viewstate off

    – And the selectedValue property of the dropdown is "", no matter what you chose. And to avoid obvious stuff, in fact putting a break point on the load method of the page, just before clicking the button would have shown the same thing…

    So, what is the matter with the dropdownlist? Why can’t it be posted to the form like any other control? I have lots of pages where I had to turn it on where I obviously didn’t need it – especially if you don’t need to show the values on the page once posted back (in my case it is redirecting to a complete different page).

    Any idea?

    Steph

  6. Steph says:

    Hi again,

    Not sure if anyone is reading or not but for those who still find that setting the EnableViewState property to True when it is obviously unnecessary is ridiculous, I have a fix:

    Remember your .asp days with the Request.Form() method? Well no matter what .net says, the dropdown is still a proper object posted to the form included in your page. And so it is possible to get its value through the Request object.

    The only gotcha is to get the Name of the control generated by .Net but fortunately the UniqueID property, that exists for any .net control, gives us just that!

    So, with a DropdownList with viewstate set to false, in the codebehind, in my click event (see example above), you can use:

    Request.Form(MyDropdownList.UniqueID)

    to retrieve the equivalent of the SelectedValue property that could only be used with ViewState set to True.

    Cheers,

    Steph

  7. Marcel says:

    Steph,

    I also thought that ViewState shouldn’t be needed but I hadn’t thought of the Request object. Some of my DropDown lists have almost 1000 entries in them and this fix has reduced the HTML considerably.

    Thanks,
    Marcel.

  8. Richard says:

    Someone is reading. Someone is also smiling at the grilling Dennis apparently gave himself?

    Thanks for the tip. One can forget the simple things.

  9. Ricardo Pinto says:

    Hey!
    I have a page where the user clicks a linkbutton, opening a javascript calendar popup. When the users clicks on a date, it inserts the selected value to a dropdownlist. This works and I can see the items being added to the dropdownlist.
    Later on I must save data and, when I click the save button, my code behind sees no items in the combobox (Me.lstExceptions.Items.Count is 0)!!! This means that I’m changing the items on the client-side, but cannot syncronize the changes to the server side, failing to save the changes. I believe that I must explicitly set EnableViewState=True to solve this… Is this correct?

    Thanks!

  10. Rob C says:

    Re: Ricardo Pinto adding items to a Drop Down List.
    I don’t think you are going to get anywhere with this even if you set the EnableViewState property to true.

    This is because the normal HTML form post data does not include all the values in a drop down … only the selected value.

    Your best bet is to add the data to a text box or hidden form field and then parse it on the server.

    If you need the user to simultaneously select one of the values then also add it to a ddl for selection. But you will have to use the Request.Form technique described above to get the selected value.

    Hope this helps but since you posted a long time ago I’m sure you have sorted it out yourself by now!

  11. Dennis,

    You need to bind your drop down list in the Init event of the page. You can then retrieve the selected value in the load or prerender events.

    Turning on viewstate simply allows the selected value to be determined in the Init event.

    Using Request.Form(MyDropdownList.UniqueID) is a good way to get the selected value early on in the init event without having to turn on viewstate which creates a lot of heavy overhead. Otherwise, if you can wait until the load or render events you don’t need to do this.

  12. Do not use the SessionState to store the information if all you want is the SelectedValue.

    Visit

    http://www.harvardinnovations.com/LearningZone/ASP_NET/DropdownlistEnableViewStateProblem

    for a detailed article on the subject

  13. Pandurang Jagtap says:

    how to store values in view state which are selected from dropdownlist

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *