Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP.NET Event Handler

ASP.NET Web Forms - Events


An event handler is a subroutine that executes code for a given event.


ASP.NET - Event Handler

Please look at the following code:

<%

lbl1.Text="The date and time is " & now()

%>

When will the code above be executed? The answer is: "Don't know...".


The Page_Load Event

The Page_Load event is one of the many events that ASP.NET understands. The Page_Load event is triggered when the page is loaded; ASP.NET automatically calls the Page_Load subroutine and executes the code within it:

Example

Demo Example »

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


The Page.IsPostBack Property

The Page_Load subroutine runs each time the page is loaded. If you only want to execute the code in the Page_Load subroutine 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 loaded for the first time; if set to true, the page has been posted back to the server (for example, by clicking a button on the form):

Example

Demo Example »

The example above 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 writes "Hello World!" in the second label, but the date and time in the first label will not change.