ASP. NET advanced programming basics Article 3-ViewState

Source: Internet
Author: User

In the previous two articles, we first learned about ASP. NET. This blog explains the most common technologies we use when developing a system or website. For example, how to reasonably use ViewState, HTTP protocol, and ASP. NET. Let's have a good cheer. Let's explain my series of lectures from instructor Yang zhongke, who I am studying now.

  1. ViewState

(1) The value of the Label version is stored in viewstate, And the TextBox version does not need to be stored. Because TextBox is input, it will be submitted to the server without hiding fields.

(2) Use asp.net to override the auto-increment of Div text (the Label Width must be increased at the same time. Note that the unit type of Width is not a simple int ).

Label. Text = (Convert. ToInt32 (Label. Text) + 1). ToString ();

Label. Width = new unit (Label. Width. Value + 10 );

(3) view the generated source code. asp.net puts all the hidden content in the hidden field named "_ VIEWSTATE" and uses the serialization algorithm to put all the hidden content in a string, click the button several times and then use the ViewStateDecoder tool to view the viewstate content. The changed content is indeed placed in the viewstate.

(4) Disable ViewState. enableviewstate = false. After viewstate is disabled, the textbox version is not affected. The Div version is affected because the input value does not rely on ViewState.

(5) When answering the ViewState principle: the Input version (TextBox auto-increment and Div version (Label) are different ).

For example, to create a Web project, drag and drop a TextBox Control and a button control, set the TextBox value to 0, click the value and width of the button TextBox to auto-increment, and write it below the button event:

1   int i = Convert.ToInt32(TextBox1.Text);2 3      i++;4 5     TextBox1.Text = i.ToString();6 7     TextBox1.Width = new Unit(TextBox1.Width.Value + 10);

 

  1. Stateless Http

(1) the Http protocol is stateless and won't remember what happened on the previous page. If you want to know the last status, one way is to save the status information to the page form before the browser response ends. The status information will be included when the next page sends a request to the server, in this way, the server can restore the last state based on the state information. The server does not remember what was last given to the browser, and the browser needs to remember these values (input is recorded in the value, for other values, put them in a hidden field, such as ViewState. When you submit the value to the server next time (increase the width by 10 ), it is to submit the last value to the server to remind him of it.

(2) Disadvantages of saving status information to hidden fields, increasing website traffic, reducing access speed, and Storing confidential data in a form may cause data spoofing and other security issues.

1 Response. write ("Label value:" + Label1.Text + "<br/>"); // If ViewState is disabled, the value of Label1.Text = "100" cannot be obtained from the client "; // even if ViewState is disabled, the value written to the browser will not be affected by 4 5 Response. write ("the Label value is" + Label1.Text + "<br/>"); // viewstate is disabled in a timely manner, and the set value can be read before the request ends.

 

  1. Use cookies in ASP. NET

(1) create two Web pages, Cookie1.aspx and Cookie2.aspx. Set Cookie on the first page, read Cookie on the second page, and put TextBox and Button controls on the first page, put the Label and Button controls in the second page, and write the following code under the Button time of the two pages:

1) page for setting values

 

1 protected void button#click (object sender, EventArgs e) 2 3 {4 5 Response. setCookie (new HttpCookie ("Color", TextBox1.Text); // you can use $. cookie value, Cookie6 7 on the server side}

 

2) page for reading values

 

1 protected void button#click (object sender, EventArgs e) 2 3 {4 5 Label1.Text = Request. Cookies ["Color"]. Value; // The server reads Cookie6 7}

 

(2) The form is page-related. Only when the browser submits the data can the server obtain it. Sometimes, you want to store visitor-related information anywhere on the server, at this time, it is not convenient to save the information to the form. Therefore, you must always remember to save the information in all the Page forms. Cookies are site-related, in addition to the form parameter sent every time a request is sent to the server, all site-related cookies will be submitted to the server. The mandatory Cookie is also saved on the browser end, in addition, the browser submits the Cookie related to the site to the server each time it requests, and forwards the Cookie returned by the server to the database. Therefore, the information can be saved in the Cookie, then, the server reads, modifies, and the server returns normal Html data. In addition, the server returns the modified Cookie. Then, the browser updates the Cookie value of the local browser.

(3) Internet optimization case: The image server and the main site domain name are different, reducing the transmission of Cookie traffic.

(4) cookies have the same disadvantages as forms, and cannot store too much information.

For example, the effect of global variables and local variables on the page

Create a web page, drag and drop a Label control and a Button control

1 private int I = 0; // every time a request comes, a new instance is added to implement the IHttpHandel Interface Class (variable 1) for processing. When it is used up, garbage collection is triggered, so the last value of 2 3 private static int j = 0 will not be kept; // All visitors are Wang Wen in the same instance 4 5 protected void Page_Load (object sender, EventArgs e) 6 7 {8 9 10 11} 12 13 protected void Button2_Click (object sender, EventArgs e) 14 15 {16 17 // I ++; 18 19 // Label1.Text = I. toString (); 20 21 j ++; 22 23 Label1.Text = j. toString (); 24 25}

 

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.