State retention scheme for clients: ViewState, hidden fields, Cookies, control status, URL query parameters
Service-side status-preserving scenarios: Session, Application, Caching (cache), database
-"ViewState
-"Example: using the Server Label control, add a button, can achieve +1?"
Looking at the source code and discovering that a span was generated, which is not able to submit data, what exactly happened?
Nature: Hidden Fields
A hidden client control named ViewState is more in the source code, storing the state
-"attribute ViewState: is a property of a collection type that inherits the key value of the control class
Storage: Data that is required on the server side but cannot be submitted to the server
Implements the server-side control
-"Disable ViewState:
Entire page disabled: ViewStateMode = viewstatemode.disabled
Single control Disabled: enableviewstate= "False"
After disabling, you can try the text box +1,label plus 1 is still valid?
-"Summary: Because hidden fields are generated and too much content is stored, it is not recommended to use
Minimize or not use ViewState, server controls
1 <%@ Page Language="C #"AutoEventWireup="true"2 ViewStateMode="Enabled"3 codebehind="ViewStateTest.aspx.cs"Inherits="t4_state.viewstatetest" %>4 5 <!DOCTYPE HTML>6 7 <HTMLxmlns= "http://www.w3.org/1999/xhtml">8 <Headrunat= "Server">9 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />Ten <title></title> One </Head> A <Body> - <formID= "Form1"runat= "Server"> - <Div> the - <Asp:buttonID= "Button1"runat= "Server"OnClick= "Button1_Click"Text= "button" /> - <Asp:labelID= "Label1"runat= "Server"Text= "0"></Asp:label> - + </Div> - </form> + </Body> A </HTML>
1 Public Partial classViewStateTest:System.Web.UI.Page2 {3 protected voidPage_Load (Objectsender, EventArgs e)4 {5 if(!IsPostBack)6 {7viewstate["YG"] ="Yang too";8 }9 }Ten One protected voidButton1_Click (Objectsender, EventArgs e) A { -Label1.Text = (int. Parse (Label1.Text) +1). ToString (); - //Label1.Text = viewstate["YG"]. ToString (); the } -}
012-viewstate State hold