Learn more about ViewState

Source: Internet
Author: User

 

Web pages are stateless, and ASP. NET pages are stateless. They are instantiated, executed, rendered, and processed during each round-trip to the server. As a Web developer, you can use well-known technologies (such as storing the status on the server in session state or uploading the page back to itself) to add states.

ViewState is a mechanism that ASP. NET uses to track the status values of server controls. Otherwise, these values are not returned as part of the HTTP form. That is to say, when the page is refreshed or returned, the control value is cleared. in cs, ViewState is often used to store values. As a storage state, ViewState replaces session state (session). For example

1General Server controls

<Asp: Label ID = "Label1" runat = "server" Text = "this is the value of Label1"> </asp: Label>

<Asp: Button ID = "Button1" runat = "server" Text = "Button"/>

If you click Button1, a button event is triggered.

Protected void button#click (object sender, EventArgs e)

    {

Label1.Text = "changing the value of Label1 ";

    }

Obviously, the value of Label1 is displayed as "changing the value of Label1". People who know about ViewState should know that this is because of ASP. NET uses this mechanism to save Label1 to the hidden domain _ VIEWSTATE. Then, the value of Label1 is directly stored in this hidden domain and assigned to Label1 to reduce the burden on the server. You can view the source file on the page, this is a base64 encoded string. It is not encrypted. More complicated, it is in the method Page. savePageStateToPersistenceMedium is saved before output and is stored by Page. loadPageStateFromPersistenceMedium loading. You can study these two methods.

<Input type = "hidden" name = "_ VIEWSTATE" id = "_ VIEWSTATE" value = "/wEPDwUJNjkxODQwNzM1ZGREm/D7KtJXowDFHnrxVg6pUDM3rQ ="/>

If it is a pure HTML control, each return will inevitably return to the initial value of the control

 

However, we can disable the ViewState mechanism, as long as it is configured on the control, page, or web. config, such

<Asp: Label ID = "Label1" runat = "server" Text = "this is Label1" EnableViewState = "False"> </asp: Label>

When you click the above button, the value of Label1 will be displayed as "this is Label1", rather than changing the value of Label1, because ViewState is responsible for retaining its value during the return period. However, because ViewState is disabled, the Label loses the value saved by ViewState, and returns to the initial value.

 

2, Special server controls

Let's take another example.

<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox> // server control

<Input type = "text" id = "TextBox2" runat = "server"/> // HTML control, but the server id is added

<Input type = "text" id = "TextBox3"/> // HTML control

<Asp: Button ID = "Button1" runat = "server" Text = "Button"/>

 

Click,

 

The first server control and the second HTML control with the server ID are found. The Page return value does not change, but the third pure HTML control value is cleared, that is, the initial value is returned, this situation has long been expected, so we continue to try the server control and try EnableViewState = "False". Let's take a look at the result.

 

Click,

 

This result is really disappointing. Obviously, ViewState has been disabled. The three control values should be empty, that is, they all return to the initial value.

ViewState is not responsible for storing changes to the values of controls inherited from the IPostBackDataHandler interface, such as TextBox.

Simply put, the value of the control inherited from the IPostBackDataHandler interface is controlled and saved by the control, but it has nothing to do with EnableViewState = false, the value of the front-end page _ VIEWSTATE is the control's own control to write _ VIEWSTATE. How to write it depends on the implementation method of the IPostBackDataHandler interface [deserialize ViewState first, and then the LoadPostData event is triggered, then, write the deserialization value to the control. For details, refer to "values. Please kindly advise]

Like

 

 

I learned in Microsoft's help documentation (http://support.microsoft.com /? Id = 316813)

 

That is to say, the IPostBackDataHandler interface is usually used to process the attributes sent by TextBox to the server. No matter whether VIEWSTATE is disabled or not, it will fill in a value in the response. Of course, this value is from the HTTP submission header.

 

3, We often tryViewstate

I often try Viewstate in aspx. cs to store data states, such

Viewstate ["name"] = "Yin Haichao";, the program then saves the Viewstate as a key-value pair, this is a process of serialization and deserialization (serialization refers to the process of converting the state information of an object into a form that can be stored or transmitted. In factObject persistenceFor example, you can save an object as binary or XML. Objects can be serialized to streams, disks, memory, and networks. On the contrary, deserialization is the process of converting the storage or transmission form into an object .), Viewstate ["name"] = "Yin Haichao" this is serialization, because serialization is actually placed into the HTML page as a string (How complicated is the intermediate process. Not yet understood); string name = Viewstate ["name"]; this is deserialization

 

4,ViewStateProcedure for assigning values to controls

 

Protected void Page_Init (object sender, EventArgs e)

{

TextBox1.Text = "bb ";

}

Protected void Page_Load (object sender, EventArgs e)

{

TextBox1.Text = "aa ";

}

Before the operation, enter cc in the first control.

 

Click the button to display

 

Execute the Page_Init () event first. TextBox1.Text will be bb first, and then execute the event called LoadViewState, here, the Page class loads values from the hidden field _ VIEWSTATE to a Label control such as the ViewState property is enabled. The LoadPostBackData event is triggered. Here, the Page class loads the value of the control inherited from the IPostBackDataHandler interface from the HTTP header (POST headers), that is, TextBox1.Text is cc, and Page_Load is executed, in this case, TextBox1.Text is aa.

 

 

Learn more about ViewState

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.