1

I've been going through this excellent article http://msdn.microsoft.com/en-us/library/ms972976.aspx that says ViewState is not responsible for form fields to retain their values between postbacks. So form fields values are never stored in ViewState?

EDITED: What I mean form fields are ASP.NET controls like TextBox, Dropdownlist etc.

EDITED: If an user enters a value in an ASP.NET textbox and submit the form, the new page still has the textbox with that value I was thinking this is because of ViewState but the article says it's not so!

VJAI
  • 32,167
  • 23
  • 102
  • 164

5 Answers5

2

As you say, form values are NOT stored in the viewstate. The reason that (for example) the text of a TextBox control is retained between two postbacks is because it implements the IPostBackDataHandler-contract and automatically maps the keys in the Request.Form-collection to the appropriate properties of the control. These two mechanisms are often confused.

See http://www.mikesdotnetting.com/Article/65/ViewState-form-fields-labels-and-Javascript for a good explanation.

Ozzy
  • 1,712
  • 11
  • 14
1

Text fields don't carry their value in ViewState because their value is explicitly sent through the HTTP POST (See Request.Form) and restored to the control before Page_Load.

DropDownLists do use ViewState to store their contents.

James McCormack
  • 9,217
  • 3
  • 47
  • 57
  • I think it's not only for textfields but for all the form controls. – VJAI Jul 21 '11 at 10:56
  • Can you plz tell me DropDownLists store what type of contents in ViewState? – VJAI Jul 21 '11 at 10:57
  • 1
    The DropDownList control renders an HTML SELECT element and a bunch of OPTION elements inside it. The HTTP POST only sends the selected value of the box. To save you having to rebind the box with options on postback (i.e. in the case that you'd need to rebind from a data store), the standard mechanism sets the options in ViewState and restores them on postback, just before the Page Load event. – James McCormack Jul 21 '11 at 11:01
  • What'll be the case if I set the options declaratively in the control? In that case the dropdownlist will get all the options at the instantiation phase itself right? not at Load ViewState. – VJAI Jul 21 '11 at 11:11
0

only Asp.Net Control will stored in the ViewState. No html fields.

So

<asp:TextBox id="tb1" runat="server" />

will work and

<input type="text" id="tb1" />

will not work.

dknaack
  • 60,192
  • 27
  • 155
  • 202
0

So form fields values are never stored in ViewState?

As stated by dknaack: No

But you may also want to have a look at ControlState since ViewState can be disabled.

Eben Roux
  • 12,983
  • 2
  • 27
  • 48
0

The point of viewstate is to track the "change" in your webcontrols. It's your responsibility as a developer to ensure that the "initial" state of your controls is recreated each page load. Then the asp .net mechanism determines the change that occurred during a postback scenario to decide which events should be fired on the server side.

For an overview of how the ASP .Net postback mechanism works and where Viewstate fits in have a look at this question I originally asked on SO;

How does Postback Work?

Community
  • 1
  • 1
Brian Scott
  • 9,221
  • 6
  • 47
  • 68