ASP. NET basics-4. ASP. NET secrets-Self-incrementing input version + 5. viewstate

Source: Internet
Author: User
ArticleDirectory
    • 4.2.set intvalue1.html as the start page

4. ASP. NET Self-incrementing input Edition

. Thinking: Set intvalue1.html as the start page

5. Preliminary study on viewstate

. Only the value attribute values of input, textarea, and select with name are submitted to the server.

. Disable viewstate

4. ASP. NET Self-incrementing input Edition
4. 1. Implement Auto-increment of input :

Click the button to add the value in input,CodeAs follows:Value = "@ Value"@ Value is custom

Intvalue1.html code

 < Form  Action  = "Intvalue1.ashx"  >          <  Input  Name  = "Ispostback"  Type  = "Hidden"  Value  = "True"  />          <  Input  Name  = "Number" Type  = "Text"  Value  = "@ Value"  /> <  Input  Type  = "Submit"  Value  = "Auto-increment"  />      </  Form  > 

Intvalue1.ashx code

 Public   Void Processrequest (httpcontext context) {context. response. contenttype = "  Text/html  "  ;  String Ispostback = context. request [ "  Ispostback  "  ];  String Number = context. request [ "  Number  " ];  If (Ispostback = "  True  " ) //  Note: If you click auto-increment, You need to auto-increment the current value.  {  Int I = Convert. toint32 (number); I ++ ; Number = I. tostring ();}  Else  // First incoming  {Number = "  0  "  ;}  String Fullpath = context. server. mappath ( "  Intvalue1.html  "  );  String Content = System. Io. file. readalltext (fullpath); Content = Content. Replace ( " @ Value  "  , Number); context. response. Write (content );} 

After three submissions

Thoughts: 4.2.set intvalue1.html as the start page

Set ashx.

5. Preliminary study on viewstate
. Only the value attribute values of input, textarea, and select with name are submitted to the server.
Why can't Div be retrieved from the server? Because it is not the server that reads the customer's webpage, but the browser collects the fields entered in the customer's form and then forms the Request Parameters and sends them to the server for processing.ProgramBecause the current innertext Of The DIV is not sent to the server, the server cannot know the current value. Do not imagine that you can submit the innertext of the Div to the server, Because only the value attribute values of input, textarea, and select with name are submitted to the server. .
 <! --  Cannot start  -->  <  Div  Name  = "AAA"  > @ Text </ Div  >   <! --  This cannot be an end.  -->  <! --  Use hidden to upload strat  -->  <  Input  Name  = "AAA"  Type  = "Hidden"  Value  = "@ Text"  />  <  Div  > @ Text </  Div  >  <! --  Use hidden to upload end  --> 

Write an auto-increment page

Auto-increment. aspx

 <  Body  >      <  Form  ID  = "Form1" Runat  = "Server"  >      <  Div  >      <  ASP: Label  ID  = "Label1"  Runat  = "Server"  Text  = "Label"  > </  ASP: Label  >         <  ASP: button  ID  = "Button1"  Runat  = "Server"  Onclick  = "Button#click"  Text  = "Button"   />      </  Div  >      </  Form  > </  Body  > 

Auto-increment. aspx. CS

 Using  System;  Using  System. Collections. Generic;  Using  System. LINQ;  Using  System. Web;  Using  System. Web. UI;  Using  System. Web. UI. webcontrols; Public   Partial   Class  Auto-increment width: system. Web. UI. Page {  Protected   Void Page_load ( Object  Sender, eventargs e ){  If (! Ispostback) {label1.text = "  0  "  ;}} Protected   Void Button#click ( Object  Sender, eventargs e ){  Int I = Convert. toint32 (label1.text); I ++ ; Label1.text = I. tostring ();}} 

ViewSource codeThe figure shows the value of viewstate.

Above value = "/wepdwuj ..." Is the value of viewstate.

Let's look at the input auto-increment. If a Textbox Control is used to replace the lable control (the code will not be written, basically the same), the value in viewstate does not store the textbox value, and the following result is obtained:

Conclusion:

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, and fields do not need to be hidden.

(Viewstatedecoder2 can be used to view the value of viewstate)

5.2 disable viewstate (no value exists in viewstate)

The result of label decompilation. viewstate is read.

Enableviewstate = false,

After viewstate is disabled, the textbox version is not affected, and the DIV version is affected because the input value does not rely on viewstate. Disable the entire page. Add enableviewstate = "false" in the ASPX page command area ".


Viewstate can be used in the Intranet system and Internet backend.

Related Article

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.