ASP. NET prevents the IsPostBack attribute loaded multiple times, asp. netispostback

Source: Internet
Author: User

ASP. NET prevents the IsPostBack attribute loaded multiple times, asp. netispostback

Check msdn, which has the IsPostBack definition above: Get a value indicating whether the page is being loaded in response to the client's sending back, or whether it is being loaded and accessed for the first time. If the page is loaded in response to the client sending backTrueOtherwise, the value is false..

First, there is a question about how web pages are loaded. The client browser server sends a request, the server sends the request to the client, and the server releases the client request.

The second question is why we should prevent multiple page loads. Based on the above loading process, we know that after clicking the submit button on the page, the browser will reload data from the server, the original data is invalid, so we only need to use the isPostBack value of true to load the page for the first time. The client's hidden control viewstate will save the client's data, that is, it does not need to be reloaded.

For example, a simple login code is as follows:

Protected void Page_Load (object sender, EventArgs e) {this.txt UserName. text = ""; this.txt UserPwd. text = ""; this. btnExit. attributes. add ("onclick", "window. close (); ");} protected void btnLogin_Click (object sender, EventArgs e) {if (this.txt UserName. text = "a" & this.txt UserPwd. text = "a") {this. lblMessage. text = "Logon successful";} else {this. lblMessage. text = "Logon Failed ";}}
The displayed interface is as follows: when you enter the wrong username and password for the first time, a message indicating logon failure is displayed.



When I re-enter the correct user name and password: The page still fails to be submitted.


The reason is that the system did not respond again after clicking log on.

Solution: change the above Code:

Protected void Page_Load (object sender, EventArgs e) {if (! Page. isPostBack) {this.txt UserName. text = ""; this.txt UserPwd. text = "";} this. btnExit. attributes. add ("onclick", "window. close (); ");} protected void btnLogin_Click (object sender, EventArgs e) {if (this.txt UserName. text = "a" & this.txt UserPwd. text = "a") {this. lblMessage. text = "Logon successful";} else {this. lblMessage. text = "Logon Failed ";}}
The changed page is displayed.



Summarized several online summaries that can be understood at the current stage:

Conclusion ① IsPostBack = false for the page migrated when Server. Transfer is used for migration.

Conclusion ② if there is no Request value in the Post method Request, that is, Request. if Form = null, IsPostBack = false; if the Get method does not contain a Request value, that is, Request. if QueryString = null, IsPostBack = false.

Conclusion ③ if QueryString or Form has a request value

Key "_ VIEWSTATE", "_ EVENTTARGET", and "_ VIEWSTATEFIELDCOUNT", and no Key is "null ", if the value starts with "_ VIEWSTATE" and there is no key-value pair with the value of "_ EVENTTARGET", IsPostBack = false.

Conclusion ④ IsPostBack = false when migrating data to the image using Response. Redirect mode.

I was just getting started with asp.net! This blog is for beginners only. If any error occurs, please correct it.

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.