Latest web development tutorials

ASP.NET event handler

It is an event handler for a given event to execute code in the subroutine.


ASP.NET - event handler

Consider the following code:

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

<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html>

The above code will be executed when? The answer is: "I do not know ...."


Page_Load event

Page_Load event is one of many events ASP.NET understandable. Page_Load event is triggered when the page is loaded, ASP.NET will automatically call the subroutine Page_Load, and executes the code:

Examples

<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" /></h3>
</form>
</body>
</html>

The demonstration >>

Note: Page_Load event contains no object references or event arguments!


Page.IsPostBack property

Page_Load subroutine runs when the page is loaded each time. If you want to perform Page_Load subroutine code when the page first loads, you can use Page.IsPostBack property. If Page.IsPostBack property is set to false, the page is first loaded. If set to true, then the page is transmitted back to the server (for example, by clicking the button on the form):

Examples

<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>
<h3><asp:label id="lbl2" runat="server" /></h3>
<asp:button text="Submit" onclick="submit" runat="server" />
</form>
</body>
</html>

The demonstration >>

The above examples show "The date and time is ...." message only when the page is first loaded. When the user clicks the Submit button is the, submit subroutine will write "Hello World!" In the second label, but the date and time of the first label will not change.