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 that 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:

  1. <%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserDetailsControl.ascx.cs"  
  2.     Inherits="UserDetailsControl" %>  
  3. <table>  
  4.     <tr>  
  5.         <td>  
  6.              First Name:  
  7.         </td>  
  8.   
  9.        <td>  
  10.             <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>  
  11.         </td>  
  12.     </tr>  
  13.     <tr>  
  14.         <td>  
  15.             Last Name:  
  16.         </td>  
  17.         <td>  
  18.             <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>  
  19.         </td>  
  20.     </tr>  
  21.     <tr>  
  22.         <td colspan="2" align="right">  
  23.             <asp:Button ID="btnClickMe" runat="server" Text="Show"  
  24.                 onclick="btnClickMe_Click" />  
  25.         </td>  
  26.     </tr>  
  27. </table>  

The following is the code behind of the user control:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;   
  7.   
  8. public partial class UserDetailsControl : System.Web.UI.UserControl  
  9. {  
  10.     private System.Delegate _delReturnData;  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.     }  
  14.     public Delegate SetMyDelegate  
  15.     {  
  16.          set  
  17.         {  
  18.             _delReturnData = value;  
  19.         }  
  20.     }  
  21.     protected void btnClickMe_Click(object sender, EventArgs e)  
  22.     {  
  23.         object[] data = new object[2];  
  24.         data[0] = txtFirstName.Text;  
  25.         data[1] = txtLastName.Text;  
  26.         _delReturnData.DynamicInvoke(data);  
  27.     }  
  28. }   

The design code of the web page:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <%@ Register Src="UserDetailsControl.ascx" TagName="UserDetailsControl" TagPrefix="uc1" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title></title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <table>  
  12.             <tr>  
  13.                <td colspan="2">  
  14.                     <uc1:UserDetailsControl ID="UserDetailsControl1" runat="server" />  
  15.                 </td>  
  16.             </tr>  
  17.             <tr>  
  18.                 <td>Address:</td>  
  19.                 <td>  
  20.                     <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>  
  21.                 </td>  
  22.             </tr>  
  23.             <tr>  
  24.                 <td>City:</td>  
  25.                 <td>  
  26.                     <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>  
  27.                 </td>  
  28.             </tr>  
  29.             <tr>  
  30.                 <td colspan="2">  
  31.                      </td>  
  32.             </tr>  
  33.             <tr>  
  34.                 <td colspan="2">  
  35.                     <asp:BulletedList ID="bulletedList" runat="server">  
  36.                     </asp:BulletedList>  
  37.                 </td>  
  38.             </tr>  
  39.         </table>  
  40.     </div>  
  41.     </form>  
  42. </body>  
  43. </html>  

The following is the code behind for it:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;   
  7.   
  8. public partial class _Default : System.Web.UI.Page  
  9. {  
  10.     private delegate void GetDetailsfromUserControl(string firstText, string secondText);  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.         GetDetailsfromUserControl delParam = new GetDetailsfromUserControl(GetData);  
  14.          UserDetailsControl1.SetMyDelegate = delParam;  
  15.     }   
  16.     private void GetData(string firstText, string secondText)  
  17.     {  
  18.          bulletedList.Items.Clear();  
  19.          bulletedList.Items.Add("First Name: " + firstText);  
  20.          bulletedList.Items.Add("Last Name: " + secondText);  
  21.          bulletedList.Items.Add("Address: " + txtAddress.Text);  
  22.          bulletedList.Items.Add("City: " + txtCity.Text);  
  23.     }  
  24. }  

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


Similar Articles