The principle of IsPostBack
--------------------------------------------------------------------------------
Take it one step at a step to make yourself understood.
Let's talk about it and then get on the code. IsPostBack: It is the page that determines whether the page is loaded for the first time or after the data is sent back (with a GET or POST request). On the code, straight point of view.
--------------------------------------------------------------------------------
1.asp.net page
--------------------------------------------------------------------------------
Copy Code code as follows:
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:button id= "Button1" runat= "Server" text= "button"/>
</div>
</form>
</body>
Copy code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
if (IsPostBack)
{
Response.Write ("This is the page after the postback!") "); Click the Button1 control to appear after this
}
Else
{
Response.Write ("This is the first loaded page!") "); This is the first time the preview appears
}
}
2.html page
--------------------------------------------------------------------------------
(1) because it is a pure HTML page, even click submit can not, postback data, that is, HTML pages can not get back the value of the postback. So IsPostBack is false.
--------------------------------------------------------------------------------
Copy Code code as follows:
<form action= "WebForm1.aspx" method= "POST" >
<input id= "Submit1" type= "Submit" value= "Submit"/>
</form>
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
if (IsPostBack)
{
Response.Write ("This is the page after the postback!") ");
}
Else
{
Response.Write ("This is the first loaded page!") "); This is the first preview, and this is what happens when you click the Submit control
}
}
(2) A hidden ViewState is added here, and the data sent back is stored in ViewState, the data postback is completed, and the IsPostBack value is true. If you're wondering what to do next time you want to load the data for the first time, I'll tell you that the next read data is read directly from the ViewState, without making a request again.
--------------------------------------------------------------------------------
Copy Code code as follows:
<form action= "WebForm1.aspx" method= "POST" >
<input type= "hidden" name= "__viewstate"/>
<input id= "Submit1" type= "Submit" value= "Submit"/>
</form>
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
if (IsPostBack)
{
Response.Write ("This is the page after the postback!") "); Click the submit control to show this again.
}
Else
{
Response.Write ("This is the first loaded page!") "); This is the first time the preview appears.
}
}
Summary: There is also a hidden viewstate field in the ASP.net page, by looking at the source on the page, you can see, generally in order to reduce the pressure on the server, we usually disable the ViewState, then will not use the IsPostBack to determine whether the data back to the page, which will be executed each time to The background code, if you read the data in the database, it will also be read every time, here you may worry about the database pressure is too large, here we have another solution, rather than using ViewState, that is, using caching technology to solve the problem here.