Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET the event handle


May 12, 2021 ASP.NET


Table of contents


ASP.NET Web Forms - Events


An event handle is a sub-routine that executes code for a given event.

When a ASP.NET event is triggered in the event, the sub-routine for that event is called. For more information, please refer to below.


ASP.NET - Event handle

Take a look at the code below:

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

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

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


Page_Load event

Page_Load event is ASP.NET one of many events that can be understood by the united states. Page_Load event is triggered when the page loads, ASP.NET will automatically call the Page_Load sublole and execute the code in it:

<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>

Demo Examples . . .

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


Page.IsPostBack property

Page_Load sub-routine runs every time the page loads. I f you only want to execute the code in the Page_Load subrouge the first time the page loads, you can use the Page.IsPostBack property. If the Page.IsPostBack property is set to false, the page is loaded for the first time, and if set to true, the page is passed back to the server (for example, by clicking the button on the 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>
<h3><asp:label id="lbl2" runat="server" /></h3>
<asp:button text="Submit" onclick="submit" runat="server" />
</form>
</body>
</html>

Demo Examples . . .

The above example only shows "The date and time is..." when the page is first loaded. N ews. When the user clicks the Submit button, the submit sub-routine will write "Hello World!" in the second label, but the date and time in the first label will not change.

The above is an ASP.NET the use of the event handle.