why we need the object sender eventargs code example

Example: What is (object sender, EventArgs e)

it is a standard in .NET that event handlers have this signature, usually declared by some delegate types for example EventHandler. sender references the object that raised the particular event, for example with Button's Click it would let you reference the Button control via sender argument.

EventArgs represents the data related to that event, and can be used to pass parameters, state information whatsoever to the event handler for the handling code to decide what to do. In this case it is just EventArgs which represents empty event arguments but for example if you have ImageButton's Click handler, you'll note that it's event arguments provide you access to the X and Y coordinates. In other words, Event args can be represented by more specific classes with event related data, depending on the control


sender is the object sender that raised the event.. like for instance if you want to get the spefic rowindex of the Button control that resides in the GridView when clicking on a partucular Button in the Grid, then you can simply cast the sender who raised the event like below.. see highlighted line below

protected void Button1_Click(object sender, EventArgs e)
{
        Button b = (Button)sender;
        string objSenderID = b.ID;
        GridViewRow row = (GridViewRow)b.NamingContainer;
        if (row != null)
        {
            int index = row.RowIndex;
        }
}

while e is the Event Argument of the object and basically contains the event data.. see the following link below for more detail information
http://msdn.microsoft.com/en-us/library/system.eventargs.aspx

Tags:

Misc Example