Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Events - What are object sender & EventArgs e?

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.

like image 653
stringo0 Avatar asked Aug 19 '09 23:08

stringo0


People also ask

What is an object sender?

Object Sender is a parameter called Sender that contains a reference to the control/object that raised the event.

What is object sender in C sharp?

The C# Object sender is one of the argument, and it's a parameter for creating the reference of the object which has been raised for the events that are used for to respond the handler to mapping the correct object but in case of static parameters or events, the value will be null with the help of EventArgs class we ...

What is sender As object E as EventArgs in VB net?

Sender is the object that raised the event and e contains the data of the event. All events in . NET contain such arguments. EventArgs is the base class of all event arguments and doesn't say much about the event.

What are EventArgs in C#?

EventArgs is also the class you use when an event does not have any data associated with it. When you create an event that is only meant to notify other classes that something happened and does not need to pass any data, include the EventArgs class as the second parameter in the delegate. You can pass the EventArgs.


1 Answers

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!";     } } 
like image 134
Noon Silk Avatar answered Nov 14 '22 00:11

Noon Silk