开发者

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 th开发者_高级运维inking this is because of ViewState but the article says it's not so!


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.


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.


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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜