Introduction to IsPostBack principles

Source: Internet
Author: User

How IsPostback works
--------------------------------------------------------------------------------

Step by step ..

Let's talk about it first, and then go to the code. Ispostback: the page that determines whether the page is loaded for the first time or after data is sent back (with a get or post request. Let's look at the code.

--------------------------------------------------------------------------------

1.asp.net page

--------------------------------------------------------------------------------
Copy codeThe Code is as follows:
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: Button ID = "Button1" runat = "server" Text = "Button"/>
</Div>
</Form>
</Body>

Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (IsPostBack)
{
Response. Write ("this is the page after sending back! "); // Click the button1 control to display this
}
Else
{
Response. Write ("This is the first page to load! "); // This is displayed in the first preview.
}
}

2.html page
--------------------------------------------------------------------------------

(1) because it is a pure html page, even if you click to submit it, you cannot send back data, that is, the html page cannot get the value of sending back. Therefore, ispostback is false.

--------------------------------------------------------------------------------
Copy codeThe Code is as follows:
<Form action = "WebForm1.aspx" method = "post">
<Input id = "Submit1" type = "submit" value = "submit"/>
</Form>

Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (IsPostBack)
{
Response. Write ("this is the page after sending back! ");
}
Else
{
Response. Write ("This is the first page to load! "); // This is displayed in preview for the first time. This is displayed after you click the submit control.
}
}

(2) A hidden viewstate is added here. The returned data is stored in viewstate, And the ispostback value is true after the data is sent back. If you are wondering what to do next time if you want to load the data for the first time, I will tell you that the next time you read the data, you can directly read it from viewstate without sending a request again.

--------------------------------------------------------------------------------
Copy codeThe Code is as follows:
<Form action = "WebForm1.aspx" method = "post">
<Input type = "hidden" name = "_ viewstate"/>
<Input id = "Submit1" type = "submit" value = "submit"/>
</Form>

Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (IsPostBack)
{
Response. Write ("this is the page after sending back! "); // Click the submit control and this will appear again.
}
Else
{
Response. Write ("This is the first page to load! "); // This is displayed in the first preview.
}
}

Summary: there is also a hidden viewstate field on the asp.net page. You can view the source code on the page. To reduce the pressure on the server, we usually disable viewstate, ispostback will not be used to determine whether the page has been sent back data. The following background code will be executed each time. If the page reads data from the database, it will also be read every time, here you may worry about the pressure on the database. Here we have another solution, instead of using viewstate, that is, using the cache technology to solve the problem.

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.