View state) is the first option to save information on a single page. The ASP. NET Web control also uses the attempt state to save the attribute values between sending and sending. Through the ViewState attribute built in the page, you can put your data into the view State set. The information types that can be saved include simple data types and custom objects. So what is the difference between the view status in the. Net development environment and the traditional view status based on COM?
View status based on COM
In traditional COM-Based ASP, when constructing the HTTP Response to be output, WEB developers need to manually refill the value of the input form part. For example, if the incoming HTTP Request contains five text boxes with specific values, * the aspx file needs to extract the current value through the Form or QueryString set of the Request object) and manually put them back into the HTTP Response stream, that is, drag and drop ). If the developer does not set this operation, five blank text boxes are displayed to the caller!
. Net-based view status
In ASP. NET, we do not need to manually delete or refill the values in the HTML part, because ASP. NET runtime will be automatically embedded in a hidden form field name is _ VIEWSTATE), its range is between the browser and a specified page. The data allocated to this field is a 64-bit encoded string that contains the most risky way to access the web parts within the scope of the INIT event handler on the current page, the _ VIEWSTATE data is used to refill the form part just before the browser that sends the corresponding response request. Obviously, the biggest advantage of ASP. NET is that no user is required. Of course, you can always interact with default functions if you want. Note that you hardcoded the items in the ListBox in the *. ASPX file. As you know, the <asp: definition in the HTML form will automatically submit their HTML code before the final HTTP response, if they have the RUNAT = "SERBVER" feature ). The simplest sentence is: Under. NET, large view State data is automatically compressed to reduce the size of the hidden form field.
View status example
The following code demonstrates how to use view status on a page. It allows you to save a series of values and restore them.
Public partial class Chapter06_ViewStateTest: System. Web. UI. Page {protected void btnSave_Clickobject sender, EventArgs e) {SaveAllTextthis. Table1.Controls, true );
}
Protected void btnRestore_Clickobject sender, EventArgs e) {RestoreAllTextthis. Table1.Controls, true );}
Private void SaveAllTextControlCollection controls, bool saveNested) {foreach Control in controls)
{If control is TextBox)
{ViewState [control. ID] = TextBox) control ). Text ;}
// The saveNested parameter of the bool type provides more flexibility for the method. www.sytm.net // you can control whether recursion is required.
If control. Controls! = Null & saveNested) {SaveAllTextcontrol. Controls, true );
}
}
}
Private void RestoreAllTextControlCollection controls, bool saveNested) {foreach Control control in controls)
{If control is TextBox)
{If ViewState [control. ID]! = Null)
{TextBox) control ). Text = ViewState [control. ID]. ToString );}
}
If control. Controls! = Null & saveNested) {RestoreAllTextcontrol. Controls, true );
}
}
}
}