ASP.NET

ASP.NET - Event Handling

ASP.NET - Event Handling

ASP.NET provides us the feature to handle the events in web Form. It allows us to implement event-based in our application. Suppose we have a button on our web page and we have to add action to that button. We can easily do this by writing a click event for the button. This can be done on both client and server-side. In ASP.NET web form, events associated with server control originates on the client-side but are resolved on the webserver by ASP.NET. 

In ASP.NET web form follows a pattern of handling events. All events pass two arguments first one is the object that raised the event and the other one is the object containing information about the event.

Application and Session events

  • Application start -It is raised when the application is started
  • Application end -It is raised when the application ends
  • Session starts – It is raised when user request for the first time
  • Session end – this is raised when the user wants to end the session

Page Control Event

  • Data Binding – this is raised when control is bound to the database
  • Disposed – this is raised when control is released 
  • Error  this called when an unhandled exception is thrown
  • Init  This is called when control is initialized 
  • Load -  this is raised when controls on the page are loaded
  • PreRender is called when the page is rendered
  • Unload  this is called when control unloaded/ released from memory after the request and response has been completed

Event Handling is done using controls

ASP. NET has classes that are fired when the user acts. For example, if you click on submit button available on the form, there exists some action that will be triggered.

ASP tag for a button control:

Code:

  <asp:Button ID="btnclick" runat="server" Text="Click" onclick="butttonclick" /> ”

Example of event handling


EventHandling.aspx.cs


Code :

EventHandling.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EnventHandling.aspx.cs" Inherits="Demo_1.EnventHandling" %>

<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
<title></title>  
<style type="text/css">  
.auto-style1 {  
width: 100%;  
        }  
.auto-style2 {  
width: 108px;  
        }  
</style>  
</head>  
<body>
      <form id="form1" runat="server">
         <div>
            <asp:Label ID="lablemessage" runat="server" >
            
            </asp:Label>
            
            <br />
            <br />
            <br />
            
            <asp:Button ID="btnclick" runat="server" Text="Click" onclick="butttonclick" />
         </div>
      </form>
   </body> 
 
</html>  

EventHandling.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Demo_1
{
    public partial class EnventHandling : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lablemessage.Text += "Page loaded event. <br />";

            if (Page.IsPostBack)
            {
                lablemessage.Text += "Page post back event.<br/>";
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            lablemessage.Text += "Page initialization event.<br/>";
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            lablemessage.Text += "Page prerender event. <br/>";
        }

        protected void button click(object sender, EventArgs e)
        {
            lablemessage.Text += "Button click event. <br/>";
        }

    }
}

Output