Implementation principle of ispostback in Asp.net

Source: Internet
Author: User

In Asp.net, The ispostback attribute is often used to determine whether it is intended to be sent back. Many people say that the pageload event must contain

Protectedvoidpage_load (objectsender, eventargs E)
{
If (! Ispostback)
{

}
}
But I really don't understand the reason. It must be true, but isn't it wrong in the previous example? Therefore, you must understand the principles ~~ To clarify this, we do not need to use HTML + handler to process Asp.net pages.ProgramTo clarify this, I will firstCodeHtml

<Htmlxmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
</Head>
<Body>
<Formaction = "hello2.ashx" method = "Post">
<Inputtype = "hidden" name = "ispostback" class = "night" value = "true"/>
Name: <inputtype = "text" id = "fasdfasd" name = "username" value = "@ value"/> <inputtype = "Submit" value = "Submit"/>
@ Msg
</Form>
</Body>
</Html>
Handler's

Publicvoidprocessrequest (httpcontext context ){
Context. response. contenttype = "text/html ";

Stringusername = context. request ["username"];
Stringmsg = "";
Stringispostback = context. request ["ispostback"]; //
If (ispostback = "true") // if this parameter is submitted, it indicates that the form is submitted.
{
Context. response. Write ("Submit to enter ");
MSG = username + "hello ";
}
Else
{
Context. response. Write ("go directly ");
Username = "";
MSG = "";
}
Stringfullpath = context. server. mappath ("hello2.htm"); // obtain the full path of the file.
Stringcontent = system. Io. file. readalltext (fullpath); // read the file
Content = content. Replace ("@ value", username );
Content = content. Replace ("@ MSG", MSG );
Context. response. Write (content );
}

Publicboolisreusable {
Get {
Returnfalse;
}
}
I believe you will understand what is going on at a Glance. I will probably describe how to run handler first because context is run for the first time. request ["ispostback"]; it must be empty. Therefore, you can directly access and then read HTML to output the content to the page (placeholder @ value and so on) to simulate Asp.net in order to save the page status)

In this case, when a vertex is submitted, the ispostback is always true because of the following statement. <inputtype = "hidden" name = "ispostback" class = "night" value = "true ""/>
This has caused many people to say that ispostback is to determine whether it is the first time you enter. In fact, after you enter the site, refresh will still find that ispostback is not true because it has not been submitted to implement a sending back.

This is a bit messy ~~ But the basic principle is as follows, hoping to help new users.

Let's take a look at the above Code and copy it and try it ~ Do not understand the following message. Welcome to discuss it.

Here is a supplement

Protectedvoidpage_load (objectsender, eventargs E)
{
If (! Ispostback)
{
// Perform data binding.
}
}
Bind data to reduce unnecessary interaction with the database.

If this parameter is not added, page_load is executed before the event is executed every time you click the button.

This causes resource fees for Data Binding and database interaction when the button is executed.

However, this judgment does not cause this problem ~~

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.