81

What do sender and eventArgs mean/refer to? How can I make use of them (for the scenario below)?

Scenario:

I'm trying to build a custom control with a delete function, and I want to be able to delete the control that was clicked on a page that contains many of the same custom control.

Albireo
  • 10,977
  • 13
  • 62
  • 96
stringo0
  • 2,720
  • 7
  • 38
  • 52
  • 1
    @rogerdeuce, we encountered this issue on a ASP.net c# project, hence it's tagged as c#. I've added the vb tag as well based on your edit comments. – stringo0 Apr 20 '15 at 16:18

5 Answers5

88

The sender is the control that the action is for (say OnClick, it's the button).

The EventArgs are arguments that the implementor of this event may find useful. With OnClick it contains nothing good, but in some events, like say in a GridView 'SelectedIndexChanged', it will contain the new index, or some other useful data.

What Chris is saying is you can do this:

protected void someButton_Click (object sender, EventArgs ea)
{
    Button someButton = sender as Button;
    if(someButton != null)
    {
        someButton.Text = "I was clicked!";
    }
}
Blue
  • 22,608
  • 7
  • 62
  • 92
Noon Silk
  • 54,084
  • 6
  • 88
  • 105
  • 4
    Thanks! Would you mind expanding on eventargs, or provide a link about them? For example, can I pass in a string value through eventargs? If so, how? – stringo0 Aug 20 '09 at 00:08
  • 6
    Well "you" are the class sending the event. So typically you subclass 'EventArgs' with your own 'MyControlsEventEventArgs' and then set properties on that for what you want to pass. So you only put things in here when you're writing the control sending the event. If you want to put a string value in the *button* you typically use the 'CommandArgument' field, which you can access by casting the sender to 'Button' (as in my post). – Noon Silk Aug 20 '09 at 00:10
23

sender refers to the object that invoked the event that fired the event handler. This is useful if you have many objects using the same event handler.

EventArgs is something of a dummy base class. In and of itself it's more or less useless, but if you derive from it, you can add whatever data you need to pass to your event handlers.

When you implement your own events, use an EventHandler or EventHandler<T> as their type. This guarantees that you'll have exactly these two parameters for all your events (which is a good thing).

Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
8

Manually cast the sender to the type of your custom control, and then use it to delete or disable etc. Eg, something like this:

private void myCustomControl_Click(object sender, EventArgs e)
{
  ((MyCustomControl)sender).DoWhatever();
}

The 'sender' is just the object that was actioned (eg clicked).

The event args is subclassed for more complex controls, eg a treeview, so that you can know more details about the event, eg exactly where they clicked.

Chris
  • 39,719
  • 45
  • 189
  • 235
  • Could you expand on this a little bit? How do I process sender/eventargs? I'm not used to using them. – stringo0 Aug 19 '09 at 23:27
  • @stringo0 sender is the object that raised the event (it calls the event delegate by passing 'this' by convention). EventArgs, if not subclassed, is of totally **no use**. So unless you have a SelectedIndexEventArgs parameter or CancelEventArgs you can't do anything. If you are not used to using them you simply don't need it but you require them in the method signature. So you can simply just ignore them. If you write a lambda to handle events you don't even need to write these arguments' names – usr-local-ΕΨΗΕΛΩΝ Jan 23 '13 at 10:52
5
  1. 'sender' is called object which has some action perform on some control

  2. 'event' its having some information about control which has some behavoiur and identity perform by some user.when action will generate by occuring for event add it keep within array is called event agrs

4

FYI, sender and e are not specific to ASP.NET or to C#. See Events (C# Programming Guide) and Events in Visual Basic.

John Saunders
  • 160,644
  • 26
  • 247
  • 397