1. Introduction
In ASP.NET Web Forms, the concept of delegates and events is used behind almost every server control —
like Button.Click, DropDownList.SelectedIndexChanged, or TextBox.TextChanged.
These are built-in events that trigger specific methods (event handlers) on the server.
But you can also create your own custom events using delegates to make your code modular and reusable.
2. What Are Delegates and Events?
| Concept | Description |
|---|---|
| Delegate | A reference type that defines the signature of a method. Think of it as a "function pointer" with safety. |
| Event | A notification mechanism that signals subscribers when something happens. Built on top of delegates. |
Real-Time Scenario
Use Case
You have an Order Processing System.
When an order is placed:
A Business Logic class (OrderManager) processes the order.
It raises an event when the order is completed.
The WebForm (UI) listens to that event and updates the user (like “Order Confirmed Successfully”).
This helps separate business logic from UI logic.
3. Step-by-Step Implementation
Folder Structure
/App_Code
└── OrderManager.cs
/OrderPage.aspx
/OrderPage.aspx.cs
Step 1: Create Delegate and Event in App_Code/OrderManager.cs
using System;
public delegate void OrderProcessedHandler(string message);
public class OrderManager
{
// Declare event
public event OrderProcessedHandler OrderProcessed;
// Method to process the order
public void ProcessOrder(string orderId)
{
// Simulate processing logic
System.Threading.Thread.Sleep(2000);
// Raise event after processing
OnOrderProcessed($"Order #{orderId} has been successfully processed!");
}
// Method to raise the event
protected virtual void OnOrderProcessed(string message)
{
if (OrderProcessed != null)
{
OrderProcessed(message); // Trigger the event
}
}
}
Step 2: Design the Web Form (OrderPage.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="OrderPage.aspx.cs" Inherits="OrderPage" %>
<!DOCTYPE html>
<html>
<head>
<title>Order Processing with Delegates and Events</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:400px; margin:auto; text-align:center; font-family:Arial;">
<h2>🛒 Order Processing System</h2>
<asp:Label ID="lblOrderId" runat="server" Text="Enter Order ID:"></asp:Label><br />
<asp:TextBox ID="txtOrderId" runat="server" Width="200px"></asp:TextBox><br /><br />
<asp:Button ID="btnProcess" runat="server" Text="Process Order" OnClick="btnProcess_Click" /><br /><br />
<asp:Label ID="lblStatus" runat="server" Text="" ForeColor="Green" Font-Bold="True"></asp:Label>
</div>
</form>
</body>
</html>
Step 3: Code Behind (OrderPage.aspx.cs)
using System;
public partial class OrderPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Nothing special on load
}
protected void btnProcess_Click(object sender, EventArgs e)
{
string orderId = txtOrderId.Text.Trim();
if (string.IsNullOrEmpty(orderId))
{
lblStatus.Text = " Please enter a valid Order ID!";
return;
}
// Create an object of OrderManager
OrderManager orderManager = new OrderManager();
// Subscribe to the event
orderManager.OrderProcessed += ShowOrderStatus;
// Start processing
lblStatus.Text = "Processing your order...";
orderManager.ProcessOrder(orderId);
}
// Event handler method
private void ShowOrderStatus(string message)
{
lblStatus.Text = message;
}
}

Join the conversation! Your thoughts help the community grow.