I got “instructed” by a friend to blog some of my more informative forum/list/newsgroup posts, especially the following one which describes the general mechanisms of events with webcontrols, so here you have it. (Names and emails removed to protected the guilty)
Here is an abbreviated look at the lifecycle:
Init
Load ViewState Data
Load PostBack Data
Load
Fire Events
PreRender
Render
To fire control events at a base level, you implement either
IPostBackDataHandler or IPostBackEventHandler.
When you implement IPostBackEventHandler, you are saying that when the
page has determined that your control was the cause of the post back it
will call your RaisePostBackEventHandler implementation during the "Fire
Events" phase. Buttons implement this, for when they are clicked. In
that method you should raise your event.
When you implement IPostBackDataHandler, you are saying that your
control has post data that you need to examine in order to determine if
an event needs to be fired. The page will call your LoadPostData
implementation during the "Load PostBack Data" phase. Here you look at
the post data to determine if you need to raise an event, but you don't
raise it now. If you return true from this, the page will call your
RaisePostDataChangedEvent implementation during the "Fire Events" phase.
Now you raise your event. Textboxes implement this interface, to
determine if they should raise their TextChanged event.
Those are the low-level event mechanisms. However, if you are
implementing a composite control, you have another, higher-level choice,
which is to handle the events raised by your child controls in order to
raise your own events. Sometimes this is done via event bubbling, other
times by creating a whole new event. To do this, progmaticly hook up the
eventhandler as you are creating the child controls. This is the way the
datagrid works.
__
Andy Smith
Chief Code Monkey
> -----Original Message-----
> Sent: Tuesday, September 23, 2003 1:50 PM
> Subject: [aspnet-controlbuilding] a custom control with events
>
>
>
> If I want to create a control with events, sort of how the
> datagrid has
> onitemdeleted, updated etc.
>
> Where would these events fire in a control I build? WOuld it
> be in the
> PreRender()? I have to create events and then allow those
> events to be able
> to added to by the developer?
>
> I realize some events will fire under cirtain circumstances
> (prob. most),
> but generally where are global events fired? PreRender?
>
> E.G MyControl.OnSomethingHappend += MyControlHandler(MyMethod);
>
> ?