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

ASP.NET event handling


May 13, 2021 ASP.NET


Table of contents


Event processing

This section introduces you to ASP.NET event handling.

An event is an action or event, such as a mouse click, a keystroke, a mouse movement, or any system-generated notification. A process communicates through events. F or example, an outage is a system-generated event. When an event occurs, the application is also able to respond and manage.

ASP.NET on the server is raised on the user's machine and processed on the server. F or example, a user clicks a button that appears in the browser. A click event is raised. The browser handles this client event by sending it to the server.

The server has a sub-program that describes what to do when an event is raised; T herefore, when event information is passed to the server, it checks to see if the click event is associated with the event handler. If there is an association, the event handler is executed.

The event parameter

ASP.NET event handler typically takes two parameters and returns empty. The first argument represents the object fire event, and the second argument represents the event parameter.

The general synth of an event is:

private void EventName (object sender, EventArgs e);

Application and session events

The most important application events are:

  • Application_Start - is raised when an application or Web page is opened.
  • Application_End - is raised when an application or web page is stopped.

Similarly, the most commonly used session events are:

  • Session_Start - When the user first requests a page from the application to be raised.
  • Session_End - is raised when the session ends.

Page and control events

Common page and control events are:

  • DataBinding - Is raised when a control is bound to a data source.
  • Disposed - is raised when a page or control is released.
  • Error - It is a page event that occurs when there are unprocessed exceptions.
  • Init - is raised when the page or control is initialized.
  • Load - Is raised when a page or control is loaded.
  • PreRender - Is raised when a page or control is displayed.
  • Unload - is raised when a page or control is unloaded from memory.

Use controls to handle events

All ASP.NET the control as a class, and when the user performs a specific action on it, they raise an event. F or example, when a user clicks a button, a 'Click' event is generated. F or handling events, there are built-in properties and event handlers. The event processing application is encoded as a response to an event and takes appropriate action against it.

By default, Visual Studio creates an event handler, including sub-programs that handle terms. This clause namer handles controls and events.

THE ASP label for the button control:

<asp:Button ID="btnCancel" runat="server" Text="Cancel" />

Event handling application for Click events:

Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) 

   Handles btnCancel.Click

End Sub

An event can also be encoded without the Handles clause. The handler must then be named based on the appropriate events that are appropriate for the control properties.

THE ASP label for the button control:

<asp:Button ID="btnCancel" runat="server" Text="Cancel" Onclick="btnCancel_Click" />

Event handling application for Click events:

Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Common control events are:

Event Property Control
Click OnClick Buttons, image buttons, link buttons, image bitmaps
Command OnCommand Buttons, image buttons, link buttons
TextChanged OnTextChanged The text box
SelectedIndexChanged OnSelectedIndexChanged Drop-down menu, list box, list of turn buttons, list box with check box
CheckedChanged OnCheckedChanged Check box, single button

Some events cause the form to be sent back to the server immediately, which is called callback events. For example, click an event like Button.Click.

Some events are not immediately sent back to the server and are referred to as non-callback events.

For example, change an event or select an event, like TextBox.TextChanged or CheckBox.CheckedChanged. These non-callback events can be called back immediately by setting their AutoPostBack property to true.

The default event

The default event for page objects is the load event. S imilarly, each control has a default event. For example, the default event for a button control is the Click event.

The default event handler can be created in Visual Studio, only by double-clicking the controls in the design view. The following table shows the default event for writing a common control:

Control The default event
Ad Controls (AdRotator) AdCreated
List of items (BulletedList) Click
Button Click
Calendar controls (Calender) SelectionChanged
Check Box CheckedChanged
CheckBoxList SelectedIndexChanged
Data Grid SelectedIndexChanged
DataList SelectedIndexChanged
DropDownList SelectedIndexChanged
HyperLink Click
Image Button (Image Button) Click
Hotspots (ImageMap) Click
Link Button (LinkButton) Click
Single or multi-select drop-down list (ListBox) SelectedIndexChanged
Menu MenuItemClick
Radio Button CheckedChanged
Radio Button Group (RadioButtonList) SelectedIndexChanged

Example

This example includes a simple page with a control label and a button control. W hen a page event, such as Page_Load, Page_Init, Page_PreRender, and so on, it sends a message that is displayed by the label control. When a button is clicked, Button_Click event is raised, and a message is also sent that is displayed by the label.

Create a new Web site and drag a label control and button control from the control tool box. U sing the window properties, set the ID of the control accordingly to .lblmessage. a nd .btnclick。 Set the text property of the button control to Click.

Mark the file (.aspx):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
   Inherits="eventdemo._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

   <head runat="server">
      <title>Untitled Page</title>
   </head>

   <body>
      <form id="form1" runat="server">
         <div>
            <asp:Label ID="lblmessage" runat="server" >

            </asp:Label>

            <br />
            <br />
            <br />

            <asp:Button ID="btnclick" runat="server" Text="Click" onclick="btnclick_Click" />
         </div>
      </form>
   </body>

</html>

Double-click the design view and move to the code behind the file. P age_Load event is created automatically and does not have any code. Write down the following lines of self-explanatory code:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

namespace eventdemo {

   public partial class _Default : System.Web.UI.Page {

      protected void Page_Load(object sender, EventArgs e) {
         lblmessage.Text += "Page load event handled. <br />";

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

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

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

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

The execution page. L abels show page load, page initialization, and page preview events. Click the button to see the effect:

ASP.NET event handling