Event handling in ASP.NET

Discussion in 'ASP.NET' started by MinalS, Jul 8, 2015.

  1. The events are raised by the client. The server handles all the event requests. If the user presses a key, the keypress event is raised. The server handles the keypress event. The event handler is a subroutine defined at the server end. The actions to be performed when an event is raised are defined in the handler.

    Once the event is raised by the client, the server checks for the availability of the appropriate handler. If the corresponding event is present, the event handler is executed.

    Event Arguments

    The event handlers in an ASP.NET application consist of two parameters and void as the return type. The syntax for an event is:

    private void EventName( object sender, EventArgs e );

    where,

    object sender represents the object raising the event
    EventArgs represents the event arguments

    Application and session states



    Application state in ASP.NET is data storage available for all the classes in the application. The state has fast access over the information stored in the database. The state is applicable for all the sessions and users.

    The Application state is saved in an instance of HttpApplicationState class. The instance is created when the user first time accesses the resources. The state is saved in the memory of the server. Hence, it is lost when user closes or restarts the application.

    The application state is not used by multiple servers in the same application. The application data can be used by many threads. The synchronization is must during the access of information from the application state.

    There are two events associated with the application state.
    1. Application_Start: When user starts application or a website, then the event is raised.
    2. Application_End: When user stops application or a website, then the event is raised.
    Session state helps user store and access values from the user as user navigates the pages in an application. The state checks for the requests from the browser in a limited span of time. The session state is enabled for all the variables.

    The variables are saved in the SessionStateItemCollection object. The Session property is used for exposing the variables.

    There are two events associated with the application state.
    1. Session_Start: When user requests for the web page from the application, the event is raised.
    2. Session_End: It is raised when the session ends.
    Page and control events

    There are several page and control events in ASP.NET. They are as mentioned below:
    1. PreInit: The event is used for recreation of dynamic controls, setting the master page and the theme property, get or set the profile values, verify the IsPostBack property.
    2. Init: The event is rasied when all the controls are initialized.
    3. InitComplete: The event is raised by the page object.
    4. PreLoad: The event is used to perform processing on the page or control before the execution of load event.
    5. Load: The event is raised when the control or the page is loaded.
    6. LoadComplete: The event is used when the controls on the page is loaded.
    7. PreRender: The event is raised before the EnsureChildControls property for the control on the page is called.
    8. Render: The Render method sends the control markup to the web browser.
    9. Unload: The event is raised for every control on the web page.

    Event handling using controls



    ASP.NET contains controls which are implemented as classes. They are associated with the events that are executed when an action is performed. There are some built in attributes and event handlers. For responding an event, a event handler is used.

    The Visual studio application contains a event handler by adding the Handles clause and a Sub procedure. The clause contains the control name and the event handled by the procedure. When user clicks a button control, the click event is generated.

    The ASP.NET button control is as shown below:

    Code:
    <asp:Button ID=”btn1” runat=”server” Text=”Show” OnClick=”btn1_Click” />
    
    An event handler is associated with the Click event. It is as shown below:

    Code:
    Protected Sub btn1_Click (ByVal sender As Object, ByVal e As System.EventArgs )
    
    End Sub
    
    Some of the common control events are as mentioned below:
    1. Click event: The OnClick attribute is executed when the button is clicked. The controls associated with the click event are Button, image button, link button and image map.
    2. TextChanged event: The OnTextChanged attribute is executed when the button is clicked. The control associated with the TextChanged event is TextBox.
    3. Command event: The OnCommand attribute is executed when the command is issued. The controls associated with the command event are image, button, link button, and Button.
    4. CheckedChangd event: The OnCheckedChanged attribute is executed when the checkmark is changed. The controls associated with the checkedchanged event are radio button and checkbox.
    5. SelectedIndexChanged event: The OnSelectedIndexChanged attribute is executed when the index is modified. The controls associated with the indexchanged event are Drop-down list, radio button list, check box list, and list box.
    There are two types of events in ASP.NET as postback and non postback events. The events which cause the form to be posted back to the server immediately are known as postback events. The non postback events are not posted back immediately back to the server.

    The AutoPostBack property is set to true for making the non poastback events as postback.

    Default events



    Every control in ASP.NET has a default event associated with it. The event handler can be created in the Visual Studio application. User needs to double click on the control present in the design view.

    Some of the controls with their default events are listed below:
    1. Button Control has Click as the default event
    2. BulletedList control has Click as the default event
    3. AdRotator control has AdCreated as the default event
    4. CheckBox control has the CheckedChanged as the default event
    5. Calendar control has the SelectionChanged as the default event
    6. CheckBoxList has the SelectedIndexChanged as the default event
    7. DataList has the SelectedIndexChanged as the default event
    8. DataGrid has the SelectedIndexChanged as the default event
    9. DropDownList has the SelectedIndexChanged as the default event
    10. ImageButton has the Click event as the default event
    11. LinkButton has the Click event as the default event
    12. Menu has MenuItemClick as the default event
    13. RadioButton has CheckChanged as the default event
    14. RadioButtonList has the SelectedIndexChanged as the default event
    Consider an example to demonstrate the different page events that occur in an application.
    1. Open Visual Studio application, click new website option.
    2. In the design view, add the label and button control from the toolbox
    3. Add appropriate ID for the controls from the properties window
    4. Add the following code in the .aspx source window.
      Code:
      <%@Page Language=”C#” AutoEventWireup=”true” CodeBehind=”Default.aspx.cs” Inherits=”demo_Default” %>
      
      <html xmlns=”http://www.w3.org/1999/xhtml”>
          
          <head runat=”server”>
              <title>Page</title>
          </head>
      
      <body>
          <form id=”form1” runat=”server”>
              <div>
              <asp:Label ID=”lbl1” runat=”server”>
              </asp:Label>
              <br/>
              <asp:Button ID=”btn1” runat=”server” Text=”Show” onclick=”btn1_Click”/>
              </div>
          </form>
      </body>
      </html>
      
    5. Navigate to the code behind file. The event Page_Load is automatically created without code.
      Code:
      using System;
      using System.Collections;
      using System.Data;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      
      namespace event
      {
          public partial class _Default : System.Web.UI.Page
          {
               protected void Page_Load(object sender, EventArgs e)
              {
                  lbl1.Text = “Page Load event”;
                  if( Page.IsPostBack)
                  {
                      lbl1.Text = “Page is not postback”;
                  }
              }
      
              protected void Page_Init(object sender, EventArgs e)
              {
                  lbl1.Text = “Page initialization event handled”;
              }
          
              protected void Page_Render(object sender, EventArgs e)
              {
                  lbl1.Text = “Page render event handled”;
              }
      
              protected void btn1_Click(object sender, EventArgs e)
              {
                  lbl1.Text = “Button click event handled”;
              }
          }
      }
      
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice