Home All Groups Group Topic Archive Search About

load event doesn't fire unless init event is present

Author
28 Jun 2005 10:41 AM
Andy Fish
Hi,

I have a custom control that is just a .cs file with no ascx file (in fact
it is a templated control so the rendered HTML is in the aspx page that
embeds it). It inherits from System.Web.UI.UserControl. It uses the standard
structure generated by visual studio with OnInit calling InitializeComponent
which initialises the event handlers.

I have found that if I include only a Load event handler it never gets
called. However, if I include both Init and Load event handlers, they both
get called as expected.

Is this a bug in the framework or have I implemented the control wrongly?

TIA

Andy

Author
7 Jul 2005 6:12 PM
Jonathan Mast
Andy,

With the exception of the Init event (which is implicitly called when the
control is first created), all Event Handlers must be "wired up" somewhere in
your code.  This is typically done in the OnInit Event Handler(which is
called when the Init event is fired).

As it would turn out, the only place that the Load Event Handler can be
wired up is in the OnInit call, since there are no events that get fired
between Init and Load (see the MSDN Article:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcontrolexecutionlifecycle.asp)
The "wiring" up of the Load eventhandler would look something like this:

this.Init+=new EventHandler(control_Init)

Best practices for development has the events getting wired up at the
earliest possible time in the page/control's lifecycle.  For Aspx pages and
User Controls this is typically in the OnInit overload; for server and
composite controls there may be some exceptions...

Hope it helps, for more information on Events and Delegates you might try
the following MSDN resource:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconeventsinwebformscontrols.asp
--
Cheers,

Jonathan



Show quoteHide quote
"Andy Fish" wrote:

> Hi,
>
> I have a custom control that is just a .cs file with no ascx file (in fact
> it is a templated control so the rendered HTML is in the aspx page that
> embeds it). It inherits from System.Web.UI.UserControl. It uses the standard
> structure generated by visual studio with OnInit calling InitializeComponent
> which initialises the event handlers.
>
> I have found that if I include only a Load event handler it never gets
> called. However, if I include both Init and Load event handlers, they both
> get called as expected.
>
> Is this a bug in the framework or have I implemented the control wrongly?
>
> TIA
>
> Andy
>
>
>
Author
7 Jul 2005 6:16 PM
Jonathan Mast
Sorry, forgot to answer your question clearly.

If the only place that the Load event can be wired up is the Init then if
you take out that Event, there is no way your Load event is getting wired up.
--
Cheers,

Jonathan



Show quoteHide quote
"Jonathan Mast" wrote:

> Andy,
>
> With the exception of the Init event (which is implicitly called when the
> control is first created), all Event Handlers must be "wired up" somewhere in
> your code.  This is typically done in the OnInit Event Handler(which is
> called when the Init event is fired).
>
> As it would turn out, the only place that the Load Event Handler can be
> wired up is in the OnInit call, since there are no events that get fired
> between Init and Load (see the MSDN Article:
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcontrolexecutionlifecycle.asp)
>  The "wiring" up of the Load eventhandler would look something like this:
>
> this.Init+=new EventHandler(control_Init)
>
> Best practices for development has the events getting wired up at the
> earliest possible time in the page/control's lifecycle.  For Aspx pages and
> User Controls this is typically in the OnInit overload; for server and
> composite controls there may be some exceptions...
>
> Hope it helps, for more information on Events and Delegates you might try
> the following MSDN resource:
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconeventsinwebformscontrols.asp
> --
> Cheers,
>
> Jonathan
>
>
>
> "Andy Fish" wrote:
>
> > Hi,
> >
> > I have a custom control that is just a .cs file with no ascx file (in fact
> > it is a templated control so the rendered HTML is in the aspx page that
> > embeds it). It inherits from System.Web.UI.UserControl. It uses the standard
> > structure generated by visual studio with OnInit calling InitializeComponent
> > which initialises the event handlers.
> >
> > I have found that if I include only a Load event handler it never gets
> > called. However, if I include both Init and Load event handlers, they both
> > get called as expected.
> >
> > Is this a bug in the framework or have I implemented the control wrongly?
> >
> > TIA
> >
> > Andy
> >
> >
> >