Asp. NET in the understanding of IsPostBack

Source: Internet
Author: User

This attribute believes that we must often use it, to determine whether it is a postback (remember this is to determine whether the postback, and heard many people say that this is the first time to load the page, and some say whether it is refreshed) many people say that the project when the Pageload event should be added, such as the following code:

protected void Page_Load (Object Sender,eventargs E)

{

if (! IsPostBack)

{

The action required to perform the first request of the page;

}

}

But I don't really understand the root of the original, said adding this is certainly true, but I do not know the ultimate will be wrong. So be sure to understand the principle ~ ~

In order to make this clear, there is no need to use the ASP. Html+handler General processing program to clarify this attribute. As follows:

<title> understanding of IsPostBack </title><body><form action= "hello.ashx" method= "POST" ><input type = "Hiddle" name= "IsPostBack" class= "Haddile" name= "IsPostBack" value= "true"/><label> name:</label><input type= "text" id= "txtUserName" name= "txtusername" value = "{userName}"/><label> Age:</label><input type= "text" id= "Txtuserage" name= "txtuserage" value = "{userage}"/><input type= "Submint" value= "Submit"/><label>{ErrorMsg}</label></form></body> The relevant HttpHandler code is as follows:Publci void Processrequset (HttpContext context){context. Response.contexttype = "text/html";string userName = context. request["txtUserName"];string userage = context. request["Txtuserage"];string msg = string. Empty;string isPostBack = context. request["IsPostBack"];if (IsPostBack = = "true")//If the parameter is submitted, it is the postback submission form making the request page {context. Response.Write ("Form submit postback Request page");msg = userName + "Hello!"; }Else{context. Response.Write ("First request page");userName = string. Empty;mag = string. Empty;userage = string. Empty;}string Fulpath = context. Server.MapPath ("hello.html");//Get File Full pathstring content = System.IO.File.ReadAllText (FullPath);//Read file contentscontent =content. Replace ("{txtusername}", userName);content = content. Replace ("{txtuserage}", userage);content = content. Replace ("{errormsg}". msg,);context. Response.Write (content);}Public bool IsReusable{get{return false;}}

  read the code and believe you'll know what's going on, here's a quick look at

run HttpHandler because the first run context.request["IsPostBack"] gets the arguments passed in must be null, So it will go directly to the page to read HTML, the first request processing data output to the HTML page.

(Placeholder {txtusername}) in order to save page State Oh.

 <input type= "hidden" name= "IsPostBack" class= "Night" value= "true"/>

ispostback is always true, so the context. request["IsPostBack"] gets the value of the parameter is "true"; This causes many people to say that IsPostBack is the first request page, in fact, when you enter the page after the refresh operation, will still find that IsPostBack is not true, The reason is that you did not commit to a postback operation.

One of the principles is that, if you have a better understanding of the explanation, you may wish to post it to share!!!

Asp. NET in the understanding of IsPostBack

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.