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"/>
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.
IPostBackDataHandlerinterface. 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
IPostBackDataHandlerinterface) 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.