ASP.NET How ViewState Works

I have a text box and a button on my .aspx page. The EnableViewState property of the text field is set to false. But when I enter the text in the text box and press the button, the entered text is still present in the text box. I expect the text box to be blank because the EnableViewState parameter is set to false. Did I miss something?

+6
viewstate
source share
3 answers

Please check this code draft article to better understand ViewState and Postback data.

This is something like:

Why do some controls persist even after disabling ViewState while others do not?

The answer is controls that implements IPostBackEventHandler IPostBackDataHandler as a Text field, checkbox, etc. state even after disabling ViewState. The reason is because the Postback Data load level, these controls will receive status information from the pending form.

But controls such as a shortcut that does not implement IPostBackEventHandler IPostBackDataHandler will not receive status information from the data sent back and, therefore, depend entirely on the viewstate to maintain the state.

Below is the relevant paragraph to your question.

There are two events in the page life cycle: related to ViewState:

  • View State: This stage follows the initialization phase of the page life cycle. At this point, the ViewState information stored in the previous postback is loaded into the control. Since there is no need to check and download previous data, when the page is loaded for the first time, the step will not happen. On subsequent page postbacks, as there may be previous data for the controls, the page will go through this step.

  • Save view state: this stage precedes the page rendering stage. At this point, the current state (value) of the controls is serialized into a 64-bit encoded string and stored in a hidden control (__ViewState) in the page.

  • Downloading the Postback Data stage: although this stage has nothing to do with ViewState, it causes most of the confusion among developers. This stage occurs only when the page has been sent back. ASP.NET controls that implement IPostBackEventHandler IPostBackDataHandler will update its value (state) from the corresponding postback data. The important things to note about this are the following:

    • The state (value) of the controls is NOT retrieved from the ViewState, but from the published reverse form.
    • The page class only passes back data to controls that implement IPostBackEventHandler IPostBackDataHandler.
    • This stage follows the stage of loading state, in other words, the state of the controls installed during viewing the load. The state stage will be overwritten at this stage.
+7
source share

It is by design

The following server controls retain their information through requests, even if the ViewState control (EnableViewState attribute) is set to False:

* The TextBox control. * The CheckBox control. * The RadioButton control. 

This is because the ViewState of the control is only one of the methods that are used to store control attributes in queries. In the server controls specified in the Problem section, attributes that are usually not sent to the server via the get form or message form are processed by ViewState. These values โ€‹โ€‹include control attributes, such as BackColor. Attributes that are typically sent to the server are handled by the IPostBackDataHandler interface. An example of such an attribute is the verified attribute of a CheckBox control.

Also read this article.

ASP.NET: TextBox and EnableViewState = "False"

For understanding Viewstate, I donโ€™t think there is a better article than MSDN

ASP.NET View State Overview

+3
source share

Take a look at Server Controls retain their state when EnableViewState is set to False

The following server controls retain their information through requests, even if the ViewState control (EnableViewState attribute) is set to False:

  • TextBox control.
  • CheckBox control.
  • RadioButton Control

This is because the ViewState of the control is only one of the methods that are used to store control attributes in queries. In the server controls mentioned, attributes that are not typically sent to the server via the get-form or message-form are processed by ViewState. These values โ€‹โ€‹include control attributes, such as BackColor.

Attributes that are typically sent to the server are handled by the IPostBackDataHandler interface. An example of such an attribute is the verified attribute of a CheckBox control.

Example : programmatically set the backcolor setting. In postback, if view mode is disabled, the background color of the Textbox control will be lost. However, the text value of the control is retained.

Note. If the backcolor was installed directly in the markup, and not in the code behind, it would be saved.

 <form id="form1" runat="server"> <asp:TextBox ID="Textbox1" runat="server" EnableViewState="false"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" EnableViewState="false" /> </form> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { this.Textbox1.BackColor = Color.Yellow; } } 

The following is an overview of the state of an ASP.NET view :

This is a common misconception among developers who view the state as somehow responsible for TextBoxes, CheckBoxes, DropDownLists, and other web controls remembering their values โ€‹โ€‹in the postback. This is not so, since values โ€‹โ€‹are identified by the values โ€‹โ€‹of the reverse form fields and are assigned in the LoadPostData () method for those controls that implement IPostBackDataHandler.

The server control may indicate that it is interested in checking pending data by implementing the IPostBackDataHandler interface . At this point in the life cycle of the page, the Page class enumerates posted back form fields and looks for the appropriate server control. If it finds a control, it checks to see if the control implements the IPostBackDataHandler interface. If so, it passes the appropriate postback data to the server control by calling the LoadPostData () method. The server element will then update its state based on postback data.

Also see below

+1
source share

All Articles