Server Control-view status and status management-Notes (6)

Source: Internet
Author: User

Web applications are created at the top layer of the HTTP protocol. This protocol is a stateless protocol.

The page and its sub-control are created based on each request, and the request is deleted at the final stage.

However, it is often necessary to maintain information beyond the duration of a Web request.

This mechanism is available in traditional ASP state management programming, such as session and cookies.

However, the session cannot be extended, and cookies cannot be relied on by all applications.

 

Asp.net provides a new mechanism-view State)

This mechanism allows the page and its sub-control to maintain status information from the server to the client, and then from the client to return.

View status provides a simple and convenient technology that allows you to create a stateful and continuously executed page in an inherent stateless environment.

Essentially, the default view State mechanism includes holding state information through an implicit variable on the page.

 

The simplest way to use the view State mechanism is to inherit the viewstate attribute from the control class through controls.

Viewstate property. Enter the system. Web. UI. statebag type-A Dictionary of key/value pairs. The value of the control property can be stored here.

The control viewstatedemolabel defines two attributes: Text and textinviewstate.

Text is stored in a private domain,

Store textinviewstate in the viewstate dictionary.

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

Namespace mspress. servercontrols
{
Public class viewstatedemolabel: webcontrol
{
Private string _ text;
Public String text {
Get {return (_ text = NULL )? String. Empty: _ text ;}
Set {_ text = value ;}
}
Public String textinviewstate
{
Get
{
Object o = viewstate ["textinviewstate"];
Return (O = NULL )? String. Empty :( string) O;
}
Set
{
Viewstate ["textinviewstate"] = value;
}
}
Protected override void rendercontents (htmltextwriter writer)
{
Writer. Write ("text = ");
Writer. Write (text );
Writer. Write ("<br> ");
Writer. Write ("textinviewstate = ");
Writer. Write (textinviewstate );
}
}

}

When the page frame reloads the home widget after the return, it will automatically re-store some attributes,

These attributes are stored in viewstate at the end of the last request processing process.

When the attribute is stored in the viewstate dictionary,

Viewstate will trace properties after the control is initialized.

Viewstate is in serializable view State of the control only when the attribute is modified after initialization,

Save the attribute value.

This process minimizes the data size during the round-trip process.

Using viewstate as property storage and custom controls, you can easily perform basic State management,

Instead of pre-defined State management Logic Programs.

If the property does not appear in the viewstate dictionary, the getter operation must return the default value related to the property.

 

The key used to store properties in viewstate, usually a string representing the property name.

 

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "default. aspx. cs" inherits = "webapplication1. _ default" %>

<% @ Register Assembly = "mspress. servercontrols" namespace = "mspress. servercontrols"
Tagprefix = "MSP" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<SCRIPT runat = "server">
Void button#click (Object sender, eventargs E)
{
Demolable1.text = textbox1.text;
Demolable1.textinviewstate = textbox2.text;
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
First name: <asp: textbox id = "textbox1" runat = "server"> </ASP: textbox> <br/>
Last name: <asp: textbox id = "textbox2" runat = "server"> </ASP: textbox>
</Div>
<Div>
<Asp: button id = "button1" runat = "server" text = "Submit" onclick = "button#click"/>
<Asp: button id = "button2" runat = "server" text = "reload"/>
</Div>
<Div>
<MSP: viewstatedemolabel id = "demolable1" runat = "server" font-size = "medium" font-names = "verdana"/>
</Div>
</Form>
</Body>
</Html>

HTML source code, there is an implicit input control named "_ viewstate". 64 base encoding string, which contains the serialized view status of the page and all controls.

Measure the test taker's knowledge about the view status of the page and each control:

<% @ Pagelanguage = "C #" trace = "true" %>

You can store multiple types in view status:

View status serialization is supported by default:

Int32, Boolean, String, unit, color.

Optimized for arrary, arrarylist, and hashtable objects of the above listed types.

Use session and application objects

Sometimes, controls may need to talk to users or even reuse data across applications.

Asp.net provides some general principles for internal sessions and application objects:

1. With view status, the storage control must restore the status data after the return.

2. Use the system. Web. sessionstate. httpsessionstate object. This object can be on the control,

Used as page. Session to store data that requires cross-user browser sessions.

Sensitive information can be stored in page. session because this information only exists on the server.

The Session object is created for each user session. When a session object is used, there is no need to worry about thread security.

However, you should pay attention to allowing page developers to disable the sesson status feature for specific Web applications.

3. Use the system. Web. httpapplicationstate object.

Page. application is used to store data that requires the entire application. This object only exists on the server.

The page. Application object is not thread-safe because multiple users can access the page at the same time.

When the page. Application object is used, it is responsible for thread security.

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.