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

stringo0 picture stringo0 · Aug 20, 2009 · Viewed 184.9k times · Source

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.

Answer

Noon Silk picture Noon Silk · Aug 20, 2009

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!";
    }
}