Page_load event of ASP. NET

Source: Internet
Author: User
Page_Load: The page load event to be executed;
Page_Load is executed in two cases;
1. The execution of the Page_Load event occurs when the page is loaded for the first time (in response to the customer's request );
2. The Page_Load event occurs when the page is sent back to the server;
When ASP. NET processes the page again, it must re-Execute Page_Load;
That is, the Page class is rebuilt, and Page_Load is the first event to be executed to recreate the Page;
Therefore, Page_Load is executed in any situation. It is necessary to determine the situation in which the server handles the Page_Load event;
Page. IsPostBack solves this problem;
In the first case (in response to the customer's request) Page. IsPostBack returns false;
In the second case (when the Page is sent back to the server for processing), Page. IsPostBack returns True;
Therefore, correct application of Page. IsPostBack can greatly improve application performance; Page_Load event
The Page_Load event is one of many events identified by ASP. NET. The Page_Load event is triggered when a page is loaded. ASP. NET automatically calls the Page_Load subprogram and runs this code internally:

<Script runat = "server">
Sub Page_Load
Lbl1.Text = "The date and time is" & now ()
End Sub
</Script>
<Html>
<Body>
<Form runat = "server">
<H3> <asp: label id = "lbl1" runat = "server"/> </Form>
</Body>
</Html>

Note: The Page_Load event does not contain object references or event parameters!

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

Page. IsPostBack attributes
The Page_Load subprogram runs every time a page is loaded. If you only want to execute code in Page_Load when loading this Page for the first time, you can use the Page. IsPostBack attribute. If the Page. the IsPostBack attribute is false. The page is loaded for the first time. If it is true, the page is "post" and returned to the server (for example, click a button in a form ):

<Script runat = "server">
Sub Page_Load
If Not Page. IsPostBack then
Lbl1.Text = "The date and time is" & now ()
End if
End Sub
Sub submit (s As Object, e As EventArgs)
Lbl2.Text = "Hello World! "
End Sub
</Script>
<Html>
<Body>
<Form runat = "server">
<H3> <asp: label id = "lbl1" runat = "server"/> <H3> <asp: label id = "lbl2" runat = "server"/> <Asp: button text = "Submit" onclick = "submit" runat = "server"/>
</Form>
</Body>
</Html>

In The above example, only The message "The date and time is..." will be written when The page is loaded for The first time. When you click the Submit button, the submit subroutine will write "Hello World!" to the second label !", However, the date and time in the first label will not change.
--------------------------------------------------------------------------------

 

Every time you click a Button, LinkButton, or ImageButton control on an ASP. NET Web page, the form is sent to the server. If the AutoPostBack attribute of some controls is set to true, after the control's status is changed, the form will also be sent back 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 .)

Each time a form is sent back to the server, it is reloaded, The page_load event is started, and all code in the page_load event processing program is executed (note that it is executed every time !).
Obviously, it is most appropriate to put the webpage initialization code here. We often want to execute some code every time a webpage is loaded, such as data binding of some controls.

When we want to execute some other code only when the webpage is loaded for the first time (basically the default binding of data), we even want some code to be executed at each load except for the first load. We can use the ispostback feature to complete this function. When a webpage is loaded for the first time, the value of this attribute is false. If the webpage is reloaded due to sending back, the value of the ispostback attribute is set to true.

In ASP. NET applications, if you need to perform initialization operations when the page is displayed for the first time, you must determine the ispostback attribute!

In ASP. net use page. ispostback, you can avoid extra work on the round-trip: If you process the Server Control to send back, you usually need to execute the code on the first request page, this code is different from the code used for round-trip when an event is triggered. If check? Page. ispostback? The code can be executed according to the conditions, depending on whether there is an initial request to the page or a response to the server control event. This seems obvious, but you can ignore this check without changing the page behavior. The usage of this attribute is directly related to whether your program runs as expected, and the efficiency of the entire page. Because, if you bind data to the control every time, no matter whether you access the control for the first time or submit the data, the efficiency of the page program can be imagined.

Each time a page with a B/S structure is submitted, it will be executed again from start to end. The C/S structure program won't be like this, which is the biggest difference with the C/S structure program! In fact, the data of the control is not obtained for this reason.

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

 

This. IsPostBack indicates whether to send the message back. The so-called sending back is that after the page is loaded, the server action is submitted on this page. this. IsPostBack = true indicates sending back.
This. IsPostBack is usually used for loading for the first time.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.