Royi Namir
Royi Namir

Reputation: 148524

TextBox values on Viewstate?

textBox values across postback do NOT maintain in Viewstate.

http://msdn.microsoft.com/en-us/library/ms972976.aspx

When the ASP.NET Web page is posted back in the load postback data stage, the Page class sees that one of the posted back form fields corresponds to the IPostBackDataHandler interface. There is such a control in the hierarchy, so the TextBox's LoadPostData() method is invoked, passing in the value the user entered into the TextBox ("Hello, World!"). The TextBox's LoadPostData() method simply assigns this passed in value to its Text property. the values are identified via posted back form field values, and assigned in the LoadPostData() method for those controls that implement IPostBackDataHandler.

Now lets talk about TextBox.OnTextChanged Method

It says there that:

Note

A TextBox control must persist some values between posts to the server for this event to work correctly. Be sure that view state is enabled for this control.

Question:

So I guess that for certain events, it does write to the Viewstate...

How does it tell ASP.NET to "Start tracking changes into the Viewsstate"?

Upvotes: 3

Views: 6421

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273199

When you enable ViewState for a TextBox, it will simply be saved there.


ASPX Page Example

 <asp:TextBox ID="tb" runat="server" EnableViewState="true"></asp:TextBox>

C# (Code-behind) Example

tb.EnableViewState = true;

Your quote talks about the fact that this value is not needed when restoring the Form on Postback.

So you can turn off ViewState for a TextBox and it will still retain its value. But OnChanged will not work.

Upvotes: 2

Alejandro B.
Alejandro B.

Reputation: 5082

I believe that the very first sentence of your question is... not wrong, but inaccurate.

You can enable/disable viewstate using the property of the control (EnableViewState) or even at page level. EnableViewState is enabled by default on TextBoxes. If it is turned on, then it WILL keep your values between postbacks.

Upvotes: 0

Related Questions