Introduction to IsPostBack principle

  • 2020-06-03 06:15:48
  • OfStack

The principle of IsPostback
--------------------------------------------------------------------------------

Step by step, let you see.

First say it, then code it up. ispostback: Determines if the page was first loaded or if the data was postback (requested by get or post). Code it, straight point.

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

1. asp. net page

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


<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>


protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                Response.Write(" This is a postback page! "); // Click on the button1 This appears after the control 
            }
            else
            {
                Response.Write(" This is the first page to load! "); // The first 1 The second preview shows this 
            }
        }

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

(1) As it is a pure html page, even if I click submit, I cannot get the postback data, that is, the html page cannot get the postback value. So ispostback is false.

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


<form action="WebForm1.aspx" method="post">
<input id="Submit1" type="submit" value="submit" />
</form>


protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                Response.Write(" This is a postback page! "); 
            }
            else
            {
                Response.Write(" This is the first page to load! "); // The first 1 The second preview shows this , Click on the submit This also appears after the control 
            }
        }

(2) A hidden viewstate is added here. The postback data is stored in viewstate, and the data postback is completed. The value of ispostback is also true. If you're wondering what to do next time you want to load the data for the first time, let me tell you, the next time you read the data, read it directly from viewstate without making another request.

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


<form action="WebForm1.aspx" method="post">
<input type="hidden" name="__viewstate" />
<input id="Submit1" type="submit" value="submit" />
</form>


protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                Response.Write(" This is a postback page! "); // Click on the submit This appears after the control. 
            }
            else
            {
                Response.Write(" This is the first page to load! "); // The first 1 The second preview shows this. 
            }
        }

Summary: It is asp net page also has a hidden viewstate fields, through can see it on the page to check the source code, in order to reduce the pressure of the server as a 1, we usually drop viewstate disabled, so will not use ispostback judge whether data sent back page, it will execute the following code every time, if it is read the data in the database, so will want to read every time 1, here you might worry about the pressure of the database is too large, here we have another solution, rather than use viewstate, And that is to use caching to solve this problem.


Related articles: