VJAI
VJAI

Reputation: 32758

Question reg. ASP.NET ViewState

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!

Upvotes: 1

Views: 221

Answers (5)

Ozzy
Ozzy

Reputation: 1730

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.

Upvotes: 2

James McCormack
James McCormack

Reputation: 9934

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.

Upvotes: 1

Brian Scott
Brian Scott

Reputation: 9361

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?

Upvotes: 0

Eben Roux
Eben Roux

Reputation: 13246

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.

Upvotes: 0

dknaack
dknaack

Reputation: 60448

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.

Upvotes: 0

Related Questions