Asp.net Server Control Development (2): Basic Application of viewstate

Source: Internet
Author: User

This articleArticleI will introduce an exciting MECHANISM OF THE Asp.net Server Control-viewstate. Why is viewstate an exciting mechanism? We know that it is necessary to maintain information beyond the duration of a Web request. In traditional ASP state management programming, we can use the session object on the server or the cookie object on the client to maintain information. However, the session state cannot be extended, and cookies cannot be used by all applications.Program. Therefore, Asp.net provides the so-called view State, that is, the iewstate Mechanism, which enables the page and its sub-controls to maintain status information during the round-trip process from the server to the client and then from the client, it can create a stateful and continuously executed page effect on an inherent stateless environment.
The simplest way to use the view State is to inherit the viewstate attribute from the control class through the control. It belongs to the system. Web. UI. statebag type and is a dictionary of key/value pairs. The following is a simple example to describe how to use viewstate. This gives us an intuitive understanding of viewstate.

Using System;
Using System. componentmodel;
Using System. Web. UI;
Using System. Web. UI. webcontrols;

Namespace 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 );
}
}
}

 

< Html >
< Head >
< Script Runat = "Server" >
Void Button#click (Object sender, eventargs E)
{
Demolabel1.text = Textbox1.text;
Demolabel1.textinviewstate = Textbox2.text;
}
</ Script >
</ Head >
< Body >
< Form Runat = "Server" >
< Mspuc: siteheader ID = "Siteheader1" Runat = "Server" Subheading = "Viewstatedemolabel test page" Heading = "Chapter 7"   />
< BR >
Enter your first name: & Nbsp;
< ASP: textbox ID = "Textbox1" Runat = "Server"   />
< BR >
Enter your last name: & Nbsp;
< ASP: textbox ID = "Textbox2" Runat = "Server"   />
< BR >
< BR >
< ASP: button Text = "Submit" Onclick = "Button#click" ID = "Button1" Runat = "Server"   />
< ASP: button Text = "Reload" Runat = "Server" ID = "Button2"   />
< BR >
< BR >
Here is the output from the viewstatedemolabel:
< BR >
< MSP: viewstatedemolabel ID = "Demolabel1" Runat = "Server" Font-names = "Verdana" Font-size = "Medium"   />
< BR >
< Mspuc: sitefooter ID = "Sitefooter1" Runat = "Server"   />
</ Form >
</ Body >
</ Html >

Two attributes are defined here. Text Properties are not used Viewstate While Textinviewstate Attribute used Viewstate . Run this sectionCode, You find that when the page Post When the page is reloaded, Text The attribute value is not saved Textinviewstate The attribute value is saved. It stores its status value at the end of the last request processing process 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.