ASP. net2.0 control status and view status 1

Source: Internet
Author: User

Basic Concepts

Control status-to make the control work normally, you sometimes need to store control status data. For example, if you write a custom control with different tabs that display different information, to make the control work as expected, the control needs to know which tab is selected during the round-trip process. The viewstate attribute can be used for this purpose, but developers may disable the view State at the page level to effectively interrupt the control. To solve this problem, the ASP. NET page framework discloses a new function called control state in ASP. NET 2.0.
The controlState attribute allows the control-Specific Property Information to be kept, unlike the viewstate attribute. To use the control status, the control must call the registerrequirescontrolstate method during initialization, And Then override the savecontrolstate and loadcontrolstate methods.

View status-view status is a method used by default by ASP. NET page framework to save the page and control value between the round-trip process. When the page is displayed in HTML format, the current status and value of the page to be retained during the sending-back process will be serialized as a base64 encoded string and output to a hidden field in the view status. By implementing a custom pagestatepersister class to store page data, you can change the default behavior and store the view status to another location (such as the SQL Server database ). For an example of storing the page status on a stream instead of a hidden page field, see the example of view State persistence mechanism.

You can use the viewstate attribute of the page to save the data in the round-trip process to the web server.Code Access view status. The viewstate attribute is a dictionary containing key/value pairs (including view State data.

Strengths and weaknesses

View status

Advantages of view status:

· You do not need to include any server resource view status in the structure of the page code.

· Implement simple view States without any custom programming. By default, status data maintenance is enabled for the control.

· Enhanced security functions the values in the view State are hashed and compressed, and encoded for Unicode implementation. Its security is higher than that of hidden fields.

Disadvantages of using view status

· Performance considerations because the view status is stored on the page itself, if a large value is stored, the speed of page display and page sending may be slowed down. Especially for mobile devices, the bandwidth is usually limited.

· Device restrictions mobile devices may not have enough memory to store a large amount of view status data.

· The potential security risk view State is stored in one or more hidden domains on the page. Although the view State stores data in a hash format, it can be tampered. If you directly view the page output source, you can see information in the hidden domain, which leads to potential security issues.

Control status

Advantages of using the control status:

· No server resources are required. By default, the control status is stored in the hidden domain on the page.

· Reliability because the control status is not as close as the view status, the control status is a more reliable way to manage the control status.

· Versatility you can write custom adapters to control the storage location of control status data and control status data.

Disadvantages of using the control status:

· Some programming is required. Although the ASP. NET page framework provides the foundation for the control state, the control state is a custom state persistence mechanism. To make full use of the control status, you must write code to save and load the control status.

Control status and view status example

This example shows how to create a custom control named indexbutton, which uses the control status to maintain key status information among multiple page requests. The control State introduced in ASP. NET 2.0 is similar to the view state, but its function is independent of the view State. Webpage developers may disable the view status of the entire page or a single control for performance reasons, but they cannot disable the control status. The control status is designed to store important data of a control (such as the number of pages of a page control, this data must be used for the control to work properly (even if the view status is disabled ). By default, the ASP. NET page framework stores the control status in a hidden element of the page, and the view status is also stored in this hidden element. Even if the view status is disabled or the session management status is used, the control status on the page is still transmitted to the client and then returned to the server. When sending a response, ASP. NET deserializes the content of the hidden elements and loads the control status to each control that has registered the control status.

This example illustrates a custom control that saves the status in both the control status and view status. In this example, the indexbutton control is derived from the button class and defines an index attribute, Which is saved in the control status. For comparison, indexbutton also defines an indexinviewstate attribute, which is stored in the viewstate dictionary. To understand the differences between the control status and view status, use Program To demonstrate the indexbutton control.

Source code of the indexbutton Control



Using system;
Using system. componentmodel;
Using system. Security. permissions;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;

Namespace customercontrols
{
[
Aspnethostingpermission (securityaction. Demand, level = aspnethostingpermissionlevel. Minimal ),
Aspnethostingpermission (securityaction. inheritancedemand, level = aspnethostingpermissionlevel. Minimal ),
Toolboxdata ("<{0}: indexbutton runat = \" Server \ "> </{0}: indexbutton> ")
]

Public class indexbutton: button
{
Private int indexvalue;
[
Bindable (true ),
Category ("behavior "),
Defaultvalue (0 ),
Description ("the index stored in control state .")
]

Public int Index
{
Get
{
Return indexvalue;
}
Set
{
Indexvalue = value;
}
}

[
Bindable (true ),
Category ("behavior "),
Defaultvalue (0 ),
Description ("the index stored in view state .")
]

Public int indexinviewstate
{
Get
{
Object OBJ = viewstate ["indexinviewstate"];
Return (OBJ = NULL )? 0: (INT) OBJ;
}
Set
{
Viewstate ["indexinviewstate"] = value;
}
}

Protected override void oninit (eventargs E)
{
Base. oninit (E );
Page. registerrequirescontrolstate (this );
}

Protected override object savecontrolstate ()
{
// Call the base class method to obtain the Base Value of the control status from the base class.
// If indexvalue is not equal and the control status of the base class is not null
// Use pair as a convenient data structure for efficient storage (and restoration in the loadcontrolstate method)
// Control status composed of two parts
Object OBJ = base. savecontrolstate ();
If (indexvalue! = 0)
{
If (OBJ! = NULL)
{
Return new pair (OBJ, indexvalue );
}
Else
{
Return (indexvalue );
}
}
Else
{
Return OBJ;
}
}

Protected override void loadcontrolstate (object state)
{
If (State! = NULL)
{
Pair P = state as pair;
If (P! = NULL)
{
Base. loadcontrolstate (P. First );
Indexvalue = (INT) p. Second;
}
Else
{
If (State is int)
{
Indexvalue = (INT) State;
}
Else
{
Base. loadcontrolstate (State );
}
}
}
}
}
}

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.