load event doesn't fire unless init event is present

A

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
 
J

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/d...guide/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/d...pguide/html/cpconeventsinwebformscontrols.asp
 
J

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.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top