Dynamic Event Handling c#

J

Jeremy Martin

Hi,

I am currently learning c# so be gentle :)

I am converting a Delphi.NET website to c# website and I am confused as
to how events are handled.

2 Situations.

1. A component on a webusercontrol fires a postback event. On postback
the parent page handles that event and runs code specifically for that
page. So that the same event can run different code depending on what
page it is on.

2. A component on a webusercontrol fires a postback event and another
webusercontrol on that page recognises that event was fired and runs
its own method.


I hope this is enough information because I only know how it works in
Delphi.

Many Thanks
Jeremy
 
G

Guest

Hi there,

You can handle an event only if you subscribed to it. Technically,
subscribing/handling means passing a method pointer to the event source:

Page.Load += new EventHandler(this.Page_Load);

If you have programmed in C or C++ is similar to this

void* (*EventHandler)(object* source, EventArgs* e);
this.Load = &this.Page_Load;

or if you programmed in Delphi Object Pascal it is equivalent to:

Page.Load := self.Page_Load;

I used similar because .NET events employ multicast delegates that are
‘collections’ of method pointer so many subscribers can handle one event.
Anyway
1. A component on a webusercontrol fires a postback event. On >postback the parent page handles that event and runs code >specifically for that page. So that the same event can run different >code depending on what page it is on.

Yes. Behind the scenes there’s always a statement that attaches event
handler to particular event. In ASP.NET 1.1, everything was explicitly
assigned in InitializeComponent() method. ASP.NET 2.0 (by default),
implicitly attaches event handlers for you based on naming convention and
AutoEventWireup @page attribute. For example, AutoEventWireup=true and page
compiler discovers there is a protected/public method called Page_Load(object
sender, EventArgs e), it automatically does assignment
Page.Load += new EventHandler(this.Page_Load);
2. A component on a webusercontrol fires a postback event and >another webusercontrol on that page recognises that event was fired >and runs its own method.

It doesn’t recognize, it just attaches an event handler as stated above.

Hope it’s clear now

Milosz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top