How to use delegate in asp.net ?


Delegate is similar to “C” language function pointer, where functions can be assigned like a variable and called at the run time based on dynamic conditions
Or
Delegate is the type that defines a method signature. To invoke a delegate object, one or more methods are required with the EXACT same signature. The delegate object will basically hold a reference of a function. The function will then can be called via the delegate object.

Uses of Delegate:
1. If we have to fire or trigger some action in our project on some particular situation like click event, then we can use delegate
2. If we have to access the main page or other usercontrol from usercontrol then we can use delegate in asp.net.

General Syntax

public delegate int Calculate(int x, int y);

Simple Example of delegate Concept with asp.net

Step1:Design the default page like above Img

Step2: Create one class i.e Class1 like this

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

/// <summary>
/// Summary description for Class1
/// </summary>
///

public delegate int Calculate(int x, int y);
public class Class1
{
public int Add(int x, int y)
{
return x + y;
}
}

Step3:Write the code on Click Event like this

protected void BtnSubmit_Click(object sender, EventArgs e)
{

Class1 mc = new Class1();
Calculate opAdd = new Calculate(mc.Add);
int res = opAdd(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
lblResult.Text = res.ToString();

}

Delegate concept with UserControl

Step1: Create one UserControl and Keep one asp.net Button Control like this

<asp:Button ID="btnTest" runat="server" Text="Click Here" OnClick="btnTest_Click" />

Step2:Write the code in Codebehind like this

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

public partial class WebUserControl : System.Web.UI.UserControl
{

// Delegate declaration
public delegate void OnButtonClick(string strValue);

// Event declaration
public event OnButtonClick btnHandler;

// Page load
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnTest_Click(object sender, EventArgs e)
{
// Check if event is null
if (btnHandler != null)
{
btnHandler(string.Empty);
}

}
}

Step3:Keep on dropdown for countrylist and label for displaying selected country and also keep the usercontrol which contain the Button like this code

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:DropDownList ID="ddlCountry" runat="server">
<asp:ListItem>Nepal</asp:ListItem>
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>Singapur</asp:ListItem>
</asp:DropDownList>

<br />
<br />
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
<br />
<br />

Selected Country is: <asp:Label ID="lblText" Text=""
runat="server"/>

</div>
</form>
</body>
</html>

Step4:Write the code in code behind page like this

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

public partial class Private : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

WebUserControl1.btnHandler+=new WebUserControl.OnButtonClick(WebUserControl1_btnHandler);

}

void WebUserControl1_btnHandler(string strValue)
{
lblText.Text = ddlCountry.SelectedItem.Text;
}
}

Step5: Now click on button which is in usercontrol,then the selected country will display on label like this

Advertisement

6 thoughts on “How to use delegate in asp.net ?

  1. satya September 16, 2012 / 4:24 pm

    // Event declaration
    public event OnButtonClick btnHandler;

    can u plz say y need of events here. then wt is these handler

  2. sudhakar September 25, 2012 / 3:11 pm

    for the above example is usefull only but images are not usefull please make it visible as soon as possible.

  3. Chandra Dev September 26, 2012 / 4:34 am

    Sorry i didnot get you. Which images are not useful ? which image are you expecting ?

    • sudhakar September 26, 2012 / 4:43 am

      can u send the real rime examples how to use in projects of windows app and web .i need some more concepts like threads,multi threads,interfaces,absrtact classes(real time examples of interface and abstract classes) ,how to use,in which scenario we will use.please send me as soon as possible.

      • Chandra Dev September 26, 2012 / 1:56 pm

        Sure, I m going to write more artical on this topic.

  4. christal_spragg June 6, 2014 / 4:22 am

    It’s a pity you don’t have a donate button! I’d deffinitely donate tto this fantastic blog!
    I guess for now i’ll settle for bookmarking and adding your RSS
    feed tto my Google account. I look firward to new updates
    and will share this webste with my Facebook group.
    Chat soon!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.