Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please explain me what is EventArgs e. Thank you for your valuable answer.
C#
protected void btnReportView_Click(object sender, EventArgs e)
   {
       Param();
       string strUrl = "wbFrmReportViewer.aspx?RepName=HolidayBillMIS&Date=" + dptdate.Text.Trim() + "&Param=" + strParam;
       Response.Redirect(strUrl);
   }
Posted

Event handling methods always has the same signature (return type + parameters).
In WPF, sender is the user control that triggers the event, maybe from a mouse click.
But in the code you don't have to use the sender variable.
It is useful if it is a textbox for example. Then you can cast the sender object into a textbox, and then access the text property. If your program only has 1 button or only 1 thing connected to a specific callback you kind of know who is the sender, so the use of it is a bit redundant.

EventArgs is a basetype, that may contain extra data.
A sender may attach a custom derived EventArg class, that contain specific info about the callback. A graphical usercontrol may for example send the X and Y coordinates. A treelistview event callback may supply info about the old selected value and the new.
In those cases, you change the signature from EventArgs to something else, or you cast it afterwards in in the method body in order to access that information. If you use the tab feature, Visual Studio should generate the correct EventArgs class

If you just have a button "Start", you usually ignore the EventArg parameter.What you want to do is call some other code. So in some cases both parameters may be ignored.
 
Share this answer
 
v2
When an event is signalled, there are two parameters which are passed through from the signaller to the handler:
sender which indicates the class or control instance which generated the signal so you can have the same handler for five different buttons for example, and determine which button it was in the handler:
C#
Button b = sender as Button;
if (b != null)
    {
    Console.WriteLine(b.Text);
    }


And

e which contains additional information about the event, and can be used as a basis for adding information that the handler needs.

The basic EventArgs has nothing particularly useful, and is often passed as null by signalling, but it can be extended by derivation to add information if it is needed.

Suppose for example you had a class which took a data feed of stock prices: You could have one instance for CompanyA and one for CompanyB, so the sender parameter would tell the handler which company to update, and you could extend the EventArgs to include the spot price: so when you got the event you could add the right data to the correct company. Even if the price changes very quickly, each time the event is signalled the actual data at that moment is preserved for the handler to work with.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900