2

I did not find .NET Events - What are object sender & EventArgs e? post much helpful.

It primarily tells that how the parameters (object sender, EventArgs e) are used in case of onClick scenario, which turns out to be the obvious use.

My question is :

Neglecting the onClick function

i.e In case of Page_Load, Init and other page events

What is the use of these (object sender, EventArgs e) parameters ?

Examples would be more helpful :-)

Community
  • 1
  • 1
Khurram W. Malik
  • 2,660
  • 2
  • 20
  • 27

2 Answers2

2

Well it's pretty simple:

object sender is the entity which emitted the event - in case of page_load i'm pretty sure it is the page itself, because the page is emitting the event (so this == sender should be true in the page). EventArgs e is boring (it's a base class), but other events bring more interesting classes which tell something about the event, so a onMouseDown-Event for example will bring data about the mouse position on screen. Other events might bring other data - but most times the Event itself and the sender are enough to get all data needed.

TGlatzer
  • 5,815
  • 2
  • 25
  • 46
  • What do you meant by **probably the page itself** ?? the number of events executed before Page_Load are 4 **Pre_Init,Init,InitComplete and Preload** what about them then ? :/ **EventArgs e is boring** does not justify things :-) because If it is there, it has some use .. If you have any idea would care to enlighten a bit more ? :-) – Khurram W. Malik Jan 23 '13 at 10:47
  • The 4 events are part of the _aspx lifecycle_ (google that if you want to know more, there are lots of events after page_load). EventArgs is boring because it's a base class, see http://msdn.microsoft.com/en-Us/library/vstudio/118wxtk3.aspx#inheritanceContinued to see which other subclasses exist, which might not be "boring" – TGlatzer Jan 23 '13 at 10:58
  • Brother I have been through that.. I simply want to say If they are lifecycle events.. there should be no need for parameters ! – Khurram W. Malik Jan 23 '13 at 11:07
  • You could do the following somewhere in an own (Custom) IHttpHandler: `var p = new MyPage(); p.OnPageLoad += MyEventHandlerForThePage;` There you would probably need the sender. – TGlatzer Jan 23 '13 at 12:32
2

Using standard(similar) signature such as (sender, eventargs) is beneficial because

  • It's flexible and generic enough to server many scenarios. Acts as a standard template even for novices
  • Similar signature increases the familiarity with the patterns, helps in reducing learning time for beginners
  • Enables re-usability at both event delegate level as well as handler level.

Now, once you accept/agree some standards, you need to stick them (even though in certain cases, it may not make sense provided that there is no huge cost associated with). .NET Fx developers has decided on this standard event template and that what's you will see everywhere.

Let's take an example of Page_Load - this event is actually declared at Control level. So its possible that some code may choose to handle multiple control's load event using the same event handler and use sender argument to apply specific things.

VinayC
  • 47,395
  • 5
  • 59
  • 72
  • Your answer covers most of it but I aint getting what would be the scenario as you stated in the last Paragraph.. Because as far as I know **ISAPI** does the calling from HttpModule to HttpHandler then the **Page events** and then finally the Module Events. How do controls can invoke the Page Events i.e. Page_Load.. Because they are executing in bulit in sequence !! Can you give an example ?? – Khurram W. Malik Jan 23 '13 at 11:04
  • @khurram, the example that I have sighted simply highlights a valid use case for having *apparently useless* sender argument for `Control.Load` event (which gets inherited as `Page.Load`). But you have missed the whole point - .NET Fx developers chose to have a standard template for their events (and you don't deviate from standard on case by case basis). – VinayC Jan 23 '13 at 11:13