The principle of IsPostback
--------------------------------------------------------------------------------
Let you understand step by step. .
Let's talk about it first, and then upload the code. ispostback: It is to determine whether the page is loaded for the first time or the page after data is posted back (with a get or post request). Let's get the code, intuitive point.
--------------------------------------------------------------------------------
page
--------------------------------------------------------------------------------
<body>
<form runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
("This is the page after postback!"); //This appears after clicking the button1 control
}
else
{
("This is the first page loaded!"); //This appears in the first preview
}
}
page
--------------------------------------------------------------------------------
(1) Since it is a pure html page, it is impossible to post back data even if you click to submit it. That is, the html page cannot get the value sent back. So ispostback is false.
--------------------------------------------------------------------------------
<form action="" method="post">
<input type="submit" value="submit" />
</form>
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
("This is the page after posting back!");
}
else
{
("This is the first page loaded!"); //This appears for the first time preview, and this appears after clicking the submit control
}
}
(2) A hidden viewstate is added here, and the data sent back is stored in the viewstate. After the data postback is completed, the value of ispostback is true. If you are wondering what to do if you want the first load data next time, I will tell you that the next time you read the data directly from the viewstate, without making a request again.
--------------------------------------------------------------------------------
<form action="" method="post">
<input type="hidden" name="__viewstate" />
<input type="submit" value="submit" />
</form>
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
("This is the page after postback!"); //This appears again after clicking the submit control.
}
else
{
("This is the first page loaded!"); //This appears in the first preview.
}
}
Summary: There is also a hidden viewstate field in the page. You can see it by viewing the source code on the page. Generally, in order to reduce the pressure on the server, we usually disable the viewstate, so we will not use ispostback to determine whether it is a page that has been sent back by data. Then the following background code will be executed every time. If you read the data in the database, you will also have to read it every time. Here you may be worried that the database is under too much pressure. Here we have another solution, instead of using viewstate, that is, using caching technology to solve the problem here.