ASP.NET Web Forms - Events
An event handler is a subroutine that executes code in response to a given event.
ASP.NET - Event Handlers
Consider the following code:
When will the above code be executed? The answer is: "I don't know...".
Page_Load Event
The Page_Load event is one of the many events that ASP.NET can understand. The Page_Load event is triggered when the page loads, and ASP.NET will automatically call the Page_Load subroutine and execute the code within it:
Example
Note: The Page_Load event does not include object references or event arguments!
Page.IsPostBack Property
The Page_Load subroutine runs every time the page loads. If you want to execute the code in the Page_Load subroutine only when the page is first loaded, you can use the Page.IsPostBack property. If the Page.IsPostBack property is set to false, the page is being loaded for the first time. If set to true, the page is being posted back to the server (for example, by clicking a button on the form):
Example
The above example displays the "The date and time is...." message only when the page is first loaded. When the user clicks the Submit button, the submit subroutine will write "Hello World!" in the second label, but the date and time in the first label will not change.