ViewState view status in ASP. NET

Source: Internet
Author: User

View status management is a simple and convenient technology. Through view status management, control developers can simulate stateful and seemingly continuous execution in an originally stateless environment. The specific process is: Whenever a user requests a. aspx page, the. NET Framework first serializes the status of the related control into a string, and then uses it as a hidden domain named _ VIEWSTATE
The Value is sent to the client. If the page is requested for the first time, the server control is also executed for the first time. The hidden domain named _ VIEWSTATE only contains the default information of the control.
It is usually null or null. In subsequent sending events, ViewState stores the available attribute states of the server control in signature sending. In this way, the server control can monitor the currently processed
The status before the delivery event occurs. These processes are the responsibility of the. NET Framework. for users, the execution of the. aspx page has the effect of continuous execution.

The most common method of view State management is to apply the ViewState attribute of the server control. ViewState is the Syetem. Web. UI. StateBag type, that is, the dictionary of a key/value pair. The attribute values of the server control can be stored in ViewState.

Public String Text
{
Get
{
Return (String) ViewState ["Text"];
}
Set
{
ViewState ["Text"] = value;
}
}

Use the set accessors to write the value of the attribute Text to the ViewState ["Text"] object. Use the get accessors
Obtain the property value in ViewState ["Text. This is the easiest way to process view states. TrackViewState,
The. NET Framework automatically manages view states such as SaveViewState and LoadViewState.

By default, ViewState can only save a few data types, including String, Int, Bool, Array, ArrayList, and HashTable. If the attribute stores other data types, you must write the view State Management Program of the sub-definition.

In addition, when ViewState is used, the object must be serialized first, and then deserialized through return. Therefore, as an excellent control developer, you must understand the ViewState
Performance content. By default, the ViewState of the control is enabled. If you do not need to use ViewState, you 'd better disable it. The following situations are no longer required
ViewState: the control does not define server events (control events are client events and do not participate in sending back). The control does not have dynamic or data-bound property values. Disable
The ViewState method is to set the value of the control's EnableViewState to "false", that is, EnableViewState = "false ".

Finally, you need to know about view State security. When the _ VIEWSTATE field is hidden, only meaningless strings are displayed. This is the. NET Framework.
The result of base64-bit encoding on the relevant content. By default, they are transmitted back and forth between the client and the server in plaintext mode. In some cases, such as passwords, accounts, and connection characters
When using string and other sensitive content, the default method is insecure. Therefore, the. NET Framework provides the following two security mechanisms for ViewState.

  • Verification Mechanism: You can set the EnableViewStateMAC = "true" attribute to instruct the. NET Framework to append a hash code to the ViewState data.
    (This hash code is a type of SHA1 with a length of 160 bits, which seriously affects execution performance ). When a callback event occurs, the hash code is re-created and must match the original hash code. This
    This method can effectively verify whether viewstate can be tampered with during transmission. By default, the. NET Framework uses the sha1 algorithm to generate the viewstate hash code. In addition,
    You can also set <machinekey> In the machine. config file to select the MD5 algorithm, for example, <machinekey
    Validation = "MD5"/>. The performance of the MD5 algorithm is better than that of the sha1 algorithm, but it is not safe enough.
  • Encryption mechanism: Use encryption to protect the actual data values in the viewstate field. First, enableviewstatemac = "true" must be set as above ". However
    Then, set the machinekey validation type to 3DES, that is, <machinekey
    Validationkey = "autogenerate" decryptionkey = "autogenerate"
    Validation = "3DES">, which indicates that ASP. net uses the 3DES encryption algorithm to encrypt the viewstate value.

The above is an excerpt from ASP. NET Server Control development technology and instance, an overview of view status management.

Solution

The view status is automatically used by the ASP. NET page framework to save the information that must be retained between each sending request. This information includes any non-default values of the control.

You can also use view status to store page-specific application data.

Function

The view status is the repository on the ASP. NET page. You can store the values that must be retained during the sending-back process. The page frame uses the view status to save the control settings between each sending and receiving.

You can use view status in your own applications to do the following:

  • Save values between each sending and receiving, instead of storing these values in the session status or user configuration file.

  • Store the value of the page or control property that you define.

  • Create a custom view status provider to store view status information in the SQL Server database or other data storage areas.

For example, you can store the information in the view State so that the code can access this information during page loading events the next time you send this page to the server. Recommended usage

Background

Web applications are stateless. Each time you access the server request page, a new instance of the webpage class is created. This usually means that all information in the page and its control will be lost during each round-trip. For example, if a user inputs information to a text box on an HTML webpage by default,

This information is sent to the server. However, this information is not returned to the browser in the response.

To overcome the inherent limitations of Web programming, the ASP. NET page framework provides several state management functions to save pages and control values to the Web server during the round-trip process. One of the features is view status.

By default, the ASP. NET page framework uses the view status to save the page and control value between the round-trip process. When the page HTML is displayed, the current status of the page and value that must be retained during the sending-back process will be serialized as a Base64 encoded string. Then, they will be put into one or more hidden fields in the page.

You can use the ViewState attribute of the page to access the view State in code. The ViewState attribute is a dictionary containing key/value pairs (including view State data.

Code

Front-end code
<Div>
Label1:
<Asp: Label ID = "Label1" runat = "server" Text = "view State canceled" EnableViewState = "false"> </asp: Label>
<Br/>
<Br/>

Label2:
<Asp: Label ID = "Label2" runat = "server" Text = "view State" EnableViewState = "true"> </asp: Label>
<Br/>
<Br/>

<Asp: Button ID = "Button1" runat = "server" Text = "set Label1, Label2 to XXX" onclick = "button#click"/>
<Br/>
<Asp: Button ID = "Button2" runat = "server" Text = "refresh"/>
</Div>

Background code

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "XXX";
Label2.Text = "XXX";
}

Execution page

Note the changes in label1 and label2 values. Because label2 has a view status, it does not change after refreshing.

The page life cycle is to load the values in the view status first, and then respond to the page event, so the buttons in step 2 are changed to "XXX, however, the view status value in label1 does not change. After you press the "refresh" button, label1 only loads the previous view status value.

Source: http://www.cnblogs.com/yangleiWPF/archive/2011/03/02/1962573.html

Related Article

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.