This article explains a common myth regarding the Viewstate and textbox.
Web is Stateless.That means it does not maintain states between two or more postbacks, all your controls values is lost. For web server, Every request is a new request. But the technology with which we work provides various techniques, mechanism to maintain state. As for example, ASP.NET provides session variables to maintain session of user between postbacks. ASP.NET also provides a mechanism to maintain the values of all the controls placed on the page through "ViewState". You can see the viewstate of any page by it's view source. (Right Click on Page -> Select View Source).
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE0OTIwMzUyNTUPZBYCZg9kFgICAw9kFgQCAQ9kFgJmD2QWAmYPZBYCAgEPDxYCHgR1JUZGQCAw9kFgQCAQ8PFgIfAAU"/>
More data, more controls leads to large view state which can drastically hit you for performance. As with every request, this data goes to server and get's updated and sent back to client. So the response size becomes more and it's hits the performance. Developers use various techniques to get rid of this problem by storing view state in database or session.
Now let's get to the point.
There is common myth that most of the asp.net developers believe that ViewState is responsible for maintaining values of controls like textbox. But that's not true. :)
There is a property in ASP.NET called "EnableViewState". This property specifies that whether you want to maintain Viewstate for a control or not. You can also specify at page level. When specified at page level, ViewState will not be maintained for all the controls of the page. By Default it is true.
Before some days I was just exploring that How EnableviewState works. I noticed that it does not work with the textbox. Let's take an example:
Put a text box with EnableViewState= “false” and a button on the page.
<asp:textbox id="TextBox1" EnableViewState="False" runat="server"/>
<asp:button id="Button1" onclick="Button1_Click" runat="server" text="Button"/>Enter some value in the text box and Click the button, a postback happens. Request goes to server and response is sent back to the client. You must be expecting that value that you have entered in Textbox will be lost and it will be empty after postback. Look at the textbox now. Oops!!! Text box is still maintaining its value. Weird.. is not it? Though You have explicitly told that ASP.NET that don't maintain textbox's viewstate but it does.
Well, Textboxes do not use ViewState to maintain/hold their values between postbacks. Values are maintained in HTTP Post headers. But what could be the reason?
Why ViewState does not maintain values?
Controls like Textbox, is inherired from
IPostBackDataHandler interface. After Page_init() events, LoadViewState event is executed which is responsible for load view state for the controls. Page class loads the ViewState from the hidden field __ViewState. After LoadViewState event, LoadPostBackData event is executed, in which values of those controls (which are inherited from
IPostBackDataHandler interface) is assigned from HTTP Post header. Server simply takes the value from the header and add an attribute to the textbox "value" which is set to the value previously entered by the user.
About the Author