Delegate

    Simple Defintion for Delegate

As written in MSDN

*********************************************************************************
“A Delegate is a type that references a method. Once a delegate is assigned a method , it behaves exactly like that method. The Delegate method can be used like any other method with parameter and return value”.

*********************************************************************************

If there is any international conference. US will send a representative u can call him\her as a delegate. He \she represents US gov. Now let’s see what an delegate is in C#.We have Single cast Delegate and multicast delegate.

When we want to call three methods on button click we use multicast delegate it has two methods combine and remove. The operator used += for combining an delegate and -= to remove an delegate.

Events

In the real world, events occur all the time. There are some that we cannot control, such as the sunrise and sunset , and there are other events that we can control.

just consider the above example

We have an timer which starts counting from 0 to 100. we need to get an notification mail or whatever when it reaches 50.how to face such a scenario?

This has to be automatic..
ButtonExample

how will you make the button to do something on its click?

What we want when any eventhappens ie(method is created)
When we click on a button an event handling takes place.Once we know what it is we want to do when the button is clicked, we must specify the method by which it can be done. These types of methods that tell a computer what to do when an event happens are called event handler methods because they “handle” an event.

<em>protected void ButtonClick(Object sender, EventArgs e)

    {

        Response.Write ("Thanks for clicking!");

    }

here we have the arguments (object sender, ImageClickEventArgs e)

We can see that sender is the object which triggered the Event to happen .in this case the button. If we want to pass any values to the code we pass it thro EventArgs

The method above gets called only if we link the button’s “click” event to it. Somewhere in the program we have to say “when this button is clicked, go to that event handler”.

protected void Page_Init(Object sender, EventArgs e)

    {

        Button1.Click += new EventHandler(ButtonClick);

    }

This line adds the Button Click method as a handler for the Click event of button1.

So rather than assigning an event handler with the assignment (=) operator, we merely add an event handler to whatever existing handlers may exist (+=). .NET does not let you use the = operator with events, and any attempt to do so will result in a compilation error. You can also use the -= operator to remove event handlers

The System.EventHandler class is a simple example of a delegate. EventHandler specifies that it must contain methods that take two parameters (an Object and an EventArgs) and return void.

So as an event of type System.EventHandler, the Click event of the Button class requires us to assign it a delegate of type Event Handler. To do this, we create a new Event Handler object containing our Button Click method with the code new Event Handler(ButtonClick), and add it to the event.

here the event handler is an example of delegate.

*********************************************************************************
Delegate can be used to pass the values from one user control to another or pass values from one usercontrol to the Container page.

We have 2 usercontrols and a Webform
Ucbaseproductlist and Ucbaseproductdetails.
Two Usercontrols are kept in the webform BaseProduct.

If we click on a particular product in Ucbaseproductlist We need to show all the details of that Product details in Ucbaseproductdetails in gridformat and there is a provision of editing and deleting the product in Ucbaseproductdetails .

So if we delete an product in Ucbaseproductdetails .it Should not be shown in Ucbaseproductlist. This is our requirement.Lets see how we can solve it using a delegate.

In the Ucbaseproductdetails We created delegate .We have an image (Delete Image) which is placed inside the grid.

Just see the code below

  public delegate void OnDeleteBaseProductHandler(int DeletedBaseProductID)
public event OnDeleteBaseProductHandler OnDelete;

Here we declare an delegate by the name “ondeletebaseproducthandler” then we create an event for that delegate and named as “OnDelete”

Here we use the keyword delegate to let the compiler know that it’s using a delegate type. An event is raised for that delegate by the name “OnDelete”.

When we click on the delete image button, we need to delete that product and bind the grid. Since the Image is placed inside the grid. we can use the item command.

in the Item command we need to pass the product id to that Event
Ie,ondelete(productID).now we have the value of product ID on the event Ondelete.

In the Web form, we are going to add a new eventhandler.

ucBaseProductList1.OnDelete+=newBaseProductList.OnDeleteBaseProductHandler(method1);

This handler method in the content page should have the same signature as of the delegate’s signature defined in UCBASEPRODUCTDETAILS .

void method1(int DeletedBaseProductID)
{
}

Now from this method we can pass the value coming from Ucbaseproductdetails to UCBASEPRODUCTLIST like this:
In this way whichever values we are selecting in UCBASEPRODUCTDETAILS will get passed to UCBASEPRODUCTLIST.

void method1(int DeletedBaseProductID)
{
ucBaseProductDetails1_OnDelete(DeletedBaseProductID);
}
Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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

%d bloggers like this: