The Page. IsPostBack attribute in ASP. NET,
Web program running mode
The client enters a URL to access a webpage on the server. After performing the corresponding operations, the client submits the webpage to the server through a form for processing. Each time the server submits a request, the corresponding program under the webpage is re-executed!
Page_Load event
Execution time. It is executed when the page is first loaded. It is executed when the page is submitted to the server for operations or when the page is refreshed.
Purpose: (1) the Page_Load event is executed every time the page is loaded. We can use it to bind data for display.
(2) Sometimes we need page initialization (first loading) to execute some programs, and we will not execute them when loading them again!
Page. IsPostBack attributes
Purpose: The server determines whether the Page is first loaded. The server returns false when it first responds to the client's request Page. IsPostBack. When the Page is submitted to the server for processing, Page. IsPostBack returns true.
If the AutoPostBack attribute of some controls is set to true, when the status of the control is changed, the form will also be submitted to the server. (AutoPostBack attribute, which has only two bool values, true/false. If this attribute is set to false, the changes will not be immediately transmitted to the server for processing after the click, and the SelectedIndexChanged event of the control will not exist .)
Example:
<Span style = "font-size: 14px;"> protected void Page_Load (object sender, EventArgs e) // The program to be executed is loaded every time on the page {// the program to be executed is loaded for the first time on the page! // (The first Page. IsPostBack is false ,! Page. IsPostBack is true. Execute the following code and load it again! Page. IsPostBack is false. The code is not executed.) if (! Page. isPostBack) {this.txt UserName. text = ""; // clear this.txt UserPwd. text = "";} // executed each time the page is loaded. this function can also be used to bind data to this. btnExit. attributes. add ("Onclick", "window. close (); ");} protected void btnLogin_Click (object sender, EventArgs e) // logon event {if (this.txt UserName. text = "a" & this.txt UserPwd. text = "a") // a simple test to determine whether the user name and password are correct {Response. write ("Logon successful"); // the user name and password are correctly displayed.} else {Response. write ("Logon Failed") ;}</span>
Summary:
Every time a form is submitted to the server, it will be reloaded, start the Page_Load event, and execute all the code in the Page_Load event processing program. the true and false attributes of IsPostBack are used to compile the code for the first execution and each loading!