I’ve seen ASP.NET programmers in their beginner states (present company included) struggle with delegating action from one control to another, or having different actions taken for the same event in different places in the web application. So I’m putting this example out there for people to better understand how that can be done using delegates in C# (personal choice…nothing against VB.NET).
The example I’m proving here solves the problem of taking action (say, a click of a button) in one control causing action in another control (could be whatever action, but simple text display in this case). So we’ll have one page Default.aspx. It will host two User Controls “ctrl.ascx” (as instance ctrl1) and “action_ctrl.ascx” (as instance action_ctrl1).
The control ctrl will contain a TextBox and a button. The control action_ctrl will contain a Label. The object is for Default.aspx to orchestrate such that when the button on ctrl1 is clicked, the Label on action_ctrl1 should be able to display the value entered in the TextBox on ctrl1.
=====================================================
——————————————–
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestDelegates._Default" %>
<%@ Register src="ctrl.ascx" tagname="ctrl" tagprefix="uc1" %>
<%@ Register src="action_ctrl.ascx" tagname="action_ctrl" tagprefix="uc2" %>
——————————————–
Default.aspx.cs
using System;
using System.Web;
using System.Web.UI;
namespace TestDelegates
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
action_ctrl1.TxtParam = ctrl1.ParamValue;
//tying the delegate instance in the first control (ctrl1) to the compatible function in the second control(action_ctrl1)
ctrl1.cbc = new ctrl.ControlButtonClick(action_ctrl1.DoAction);
}
}
}
——————————————–
ctrl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ctrl.ascx.cs" Inherits="TestDelegates.ctrl" %>
TextBox ID="TextBox1" runat="server">
——————————————–
ctrl.ascx.cs
using System;
using System.Web;
using System.Web.UI;
namespace TestDelegates
{
public partial class ctrl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
//tying the PUBLIC delegate instance to the button click event so that it can be used outside of this class
Button1.Click+=new EventHandler(cbc);
}
//declaring the delegate type
public delegate void ControlButtonClick(object sender, EventArgs e);
//declaring the delegate instance
public ControlButtonClick cbc;
public string ParamValue
{
get
{
return TextBox1.Text;
}
}
}
}
——————————————–
action_ctrl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="action_ctrl.ascx.cs" Inherits="TestDelegates.action_ctrl" %>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
——————————————–
action_ctrl.ascx.cs
using System;
using System.Web;
using System.Web.UI;
namespace TestDelegates
{
public partial class action_ctrl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string TxtParam { get; set; }
//declaring the function compatible to the delegate
public void DoAction(object sender, EventArgs e)
{
Label1.Text = "You entered Value " + TxtParam;
}
}
}
——————————————–
=====================================================
So, when I view the page (default.aspx), enter a value in the TextB0x and click on the button (on ctrl.ascx), the Label (on action_ctrl.ascx) gets populated with the same text!

Delegates Code Running
Now we’ve shown how an action in one control can result in reaction in another control, all orchestrated by a parent page containing the two controls.
Now we can implement the click in as many controls as you want.
Lets have some fun shall we:
Let us create a third User Control another_action_ctrl.ascx. We can put a Label on it and do whatever we want with the same value on that control.
=====================================================
——————————————–
another_action_ctrl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="another_action_ctrl.ascx.cs" Inherits="TestDelegates.another_action_ctrl" %>
<asp:Label ID="Label1" runat="server" Text="You entered:"></asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
——————————————–
another_action_ctrl.ascx.cs
using System;
using System.Web;
using System.Web.UI;
namespace TestDelegates
{
public partial class another_action_ctrl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string TxtParam { get; set; }
//declaring the function compatible to the delegate
public void DoAction(object sender, EventArgs e)
{
TextBox1.Text = TxtParam;
}
protected void Button1_Click(object sender, EventArgs e)
{
//you can do whatever pleases you on here....or maybe you can "delegate" this on to another control
}
}
}
——————————————–
Display.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestDelegates._Default" %>
<%@ Register Src="ctrl.ascx" TagName="ctrl" TagPrefix="uc1" %>
<%@ Register Src="action_ctrl.ascx" TagName="action_ctrl" TagPrefix="uc2" %>
<%@ Register Src="another_action_ctrl.ascx" TagName="another_action_ctrl" TagPrefix="uc3" %>
——————————————–
Display.aspx.cs
using System;
using System.Web;
using System.Web.UI;
namespace TestDelegates
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
action_ctrl1.TxtParam = ctrl1.ParamValue;
another_action_ctrl1.TxtParam = ctrl1.ParamValue;
//tying the delegate instance in the first control (ctrl1) to the compatible function in the second control(action_ctrl1)
ctrl1.cbc = new ctrl.ControlButtonClick(action_ctrl1.DoAction);
ctrl1.cbc2 = new ctrl.ControlButtonClick(another_action_ctrl1.DoAction);
}
}
}
——————————————–
=====================================================
So lets view the page and enter some text and see what happens…

Delegate Running 2
So what happened?
Basically we’ve translated the click of a button (on ctrl.asx) into action on two different user controls one after another. We could use “BeginInvoke” and “EndInvoke” in Default.aspx to and have more fun, but I’ll leave that to you to explore….don’t want me having all the fun do we now
So we’ve seen how awesome delegates can be in C# and ASP.NET. I hope this persuades beginners to explore the world of Delegates some more.
Feel free to leave any comments, suggest improvements, or point out goof-ups!