views:

2856

answers:

5

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.

A: 

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
Could you expand on this a little bit? How do I process sender/eventargs? I'm not used to using them.
stringo0
+5  A: 

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 btSomeButton_Click (object sender, EventArgs ea){
   Button btnSomeButton = sender as Button;
   btnSomeButton.Text = "I was clicked!";
}
Noon Silk
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
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
Thanks for the extra info!
stringo0
+2  A: 

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 should 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
+1  A: 

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

rajeev kumar singh