Event handler is a method, which executes the code in response to a specific event that occurs in an application.
It is used in GUI (Graphical User Interface) applications to handle events, which are raised by the controls of the interface.
In ASP.NET Events,One single event handler can be used to handle events raised by multiple controls of the interface.
Page_Load Event
Description
Page_Load event is an event that is triggered every time the page loads. ASP.NET automatically calls this method and executes the code that is written under this.
[html]
<script runat="server">
Sub Page_Load
splbl.Text="Welcome To SPLessons..!!" & spMethod()
End Sub
</script>
<html>
<body>
<form runat="server">
<h1><asp:label id="splbl" runat="server" /></h1>
</form>
</body>
</html>
[/html]
In the above code, one can see a label is set with the text “Welcome To SPLessons..!!” and it is under Page_Load. Whenever, the page gets loaded this label will be displayed and the code written under spMethod() method will be called.
Below is the structure of the Page_Load event from the .aspx.cs file.
[csharp] protected void Page_Load(object sender, EventArgs e)
{
//....code
}
[/csharp]
PostBack Events
Description
Suppose when a page is requested for the first time, the UI with three controls like a textbox, a button and a label, is rendered. This is the first request for the page. When, some text is entered in the text box and button is clicked, the information entered in the text box is sent to the server and the code gets executed, which shows the entered text on the label. Here, the information is entered on the client side and it is processed by the server.
One need to know how this information is sent to server. The information is sent to server via ‘Post’ method of HTTP request. When, one request back the page second time using HTTP post request. This is called PostBack, and can execute the code in the Page_Load subroutine only for the first time the page is loaded using the Page.IsPostBack property. If the Page.IsPostBack property is false, the page is loaded for the first time and if it is true, the page is posted back to the server.
Example
In the below example, consider a form where on clicking of "Submit" button entered values will be store in a database. For this, one have to write code under ButtonClick event of "Submit" button.
Code will be written under button click event.
[csharp]protected void spbtnSubmit_Click(object sender, EventArgs e)
{
// .....code
}[/csharp]