load event doesn't fire unless init event is present

Discussion in 'ASP .Net Web Controls' started by Andy Fish, Jun 28, 2005.

  1. Andy Fish

    Andy Fish Guest

    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
    Andy Fish, Jun 28, 2005
    #1
    1. Advertising

  2. 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
    --
    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
    >
    >
    >
    Jonathan Mast, Jul 7, 2005
    #2
    1. Advertising

  3. 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



    "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/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
    > --
    > 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
    > >
    > >
    > >
    Jonathan Mast, Jul 7, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.Marsch

    Init event does not fire

    J.Marsch, Dec 13, 2003, in forum: ASP .Net
    Replies:
    9
    Views:
    3,145
    J.Marsch
    Dec 18, 2003
  2. Shimon Sim

    Init event for control doesn't fire

    Shimon Sim, Aug 12, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    441
    Steven Cheng[MSFT]
    Aug 17, 2005
  3. Robert Oschler
    Replies:
    0
    Views:
    674
    Robert Oschler
    Jul 20, 2003
  4. Ralf Wahner
    Replies:
    5
    Views:
    627
    Bob Foster
    Dec 24, 2003
  5. Gábor SEBESTYÉN

    Unless unless

    Gábor SEBESTYÉN, Jun 17, 2005, in forum: Ruby
    Replies:
    3
    Views:
    148
    Gábor SEBESTYÉN
    Jun 17, 2005
Loading...

Share This Page