Asp.net decryption 1: page re-painting, ispostback

Source: Internet
Author: User

For beginners, take care of them.

The http protocol is stateless. When you click the submit button, it is submitted to the processing page. If the current page is processed, In the aspx page, the result is a combination of the page before submission and the returned result after processing. The internal implementation principle is actually as follows:

After clicking submit, the user reads the content of the original page and writes it back to the page. For example, the text box auto-increment function:

1. Create a new HTML page named incnum.htm as the template.

<Html>

<Body>

<Form action = "IncNum. ashx">

<Input type = "text" name = "txtnum" value = "@ balue"/>

<Input type = "hidden" name = "IsPostBack" value = "true"/>

<Input type = "submit" value = "auto-increment"/>

</Form>

</Body>

</Html>

2. Create an ashx page as the processing page IncNum. ashx. The processing code is as follows:

Public void ProcessRequest (HttpContext context ){
Context. Response. ContentType = "text/html ";
String ispostback = context. Request ["IsPostBack"];

String value = "0 ";

If (ispostback = "true") // Yes

{

Int num = Convert. ToInt32 (context. Request ["txtnum"]);

Num ++;

Value = num. TOString ();

}

String fullpath = context. Server. MapPath ("IncNum.htm"); // obtain the path of the template file

String content = File. ReadAllText (fullpath); // read the Template File

Content = content. Replace ("@ value", num); Replace the self-added content

Context. Response. write (content );

}
 
Public bool IsReusable {
Get {
Return false;
}
}

}

 

 

 

Explanation 1: IsPostBack is actually a hidden field in the form. Its value is fixed to true. When you click the "auto-increment" button, IspostBack is also submitted to the server, after reading this value, the handler can determine that the page is submitted and then read the submitted value for processing. If the page cannot be read, the handler can fool the server for the first time: enter the address http: // localhost: 1128/IncNum in the browser. ashx? If IsPostBack = true, the handler treats the page as the submitted page.

IsPostBack on the aspx page is implemented using this principle.

Explanation 2: The ashx page is displayed to the user. The html page is a template page and the user cannot see it. The @ value in the html page is equivalent to a placeholder. The ashx page reads the html page, use the string replacement method to replace the placeholder with the processed value. Then re-write it back to the page, which is why the submitted page will remain as original.

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.