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.