Using Delegates in ASP.NET

Delegates

Delegates have always been a useful concept introduced in the .Net framework, but because of the difficulty of implementation, first-time developers might find it difficult to implement delegates in .Net programs. However this is just a simple and small demo introduced which uses delegates for retrieving data from a user control and displaying it on a web page.

The scenario is something like this; we have a user control which consists of two fields, the first name and last name and a button on it and we have one web page where we have used this control along with two page fields, which are address and city. Now on a web page we have a total of 4 fields; 2 from the user control and 2 which are present on a web page. On click of the button (which is there on the user control) the data should be displayed on the web page of all the fields from the user control and that of the webpage.

 Here the main concern is how to read the data from the user control; for reading the data a delegate is used.

Here is the diagrammatic representation of the web page:

Delegates-in-Asp.Net.jpg

In the above diagram are the first name and last name and the show button; they are the fields of the user control. Address and city are the fields on the webpage. On click of the button the data should be displayed on the web page like the following.

So without any delay I'll move toward the source code and the design of it.

The design code of the user control is:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserDetailsControl.ascx.cs"
    Inherits="UserDetailsControl" %>
<table>
    <tr>
        <td>
             First Name:
        </td>

       <td>
            <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            Last Name:
        </td>
        <td>
            <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td colspan="2" align="right">
            <asp:Button ID="btnClickMe" runat="server" Text="Show"
                onclick="btnClickMe_Click" />
        </td>
    </tr>
</table> 

The following is the code behind of the user control:

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

public partial class UserDetailsControl : System.Web.UI.UserControl
{
    private System.Delegate _delReturnData;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public Delegate SetMyDelegate
    {
         set
        {
            _delReturnData = value;
        }
    }
    protected void btnClickMe_Click(object sender, EventArgs e)
    {

        object[] data = new object[2];
        data[0] = txtFirstName.Text;
        data[1] = txtLastName.Text;
        _delReturnData.DynamicInvoke(data);

    }
} 

The design code of the web page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="UserDetailsControl.ascx" TagName="UserDetailsControl" TagPrefix="uc1" %>
<!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></title>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
               <td colspan="2">
                    <uc1:UserDetailsControl ID="UserDetailsControl1" runat="server" />
                </td>
            </tr>
            <tr>
                <td>Address:</td>
                <td>
                    <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>City:</td>
                <td>
                    <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                </td>
            </tr>

            <tr>
                <td colspan="2">
                    &nbsp;</td>
            </tr>
            <tr>
                <td colspan="2">

                    <asp:BulletedList ID="bulletedList" runat="server">
                    </asp:BulletedList>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html> 

The following is the code behind for it:

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

public partial class _Default : System.Web.UI.Page
{
    private delegate void GetDetailsfromUserControl(string firstText, string secondText);
    protected void Page_Load(object sender, EventArgs e)
    {
        GetDetailsfromUserControl delParam = new GetDetailsfromUserControl(GetData);
         UserDetailsControl1.SetMyDelegate = delParam;
    } 

    private void GetData(string firstText, string secondText)
    {

         bulletedList.Items.Clear();
         bulletedList.Items.Add("First Name: " + firstText);
         bulletedList.Items.Add("Last Name: " + secondText);
         bulletedList.Items.Add("Address: " + txtAddress.Text);
         bulletedList.Items.Add("City: " + txtCity.Text);
    }
}

I hope this example might help you in your program and you have enjoyed it. For any queries feel free to post questions.