Can web control events fire FIRST?

Discussion in 'ASP .Net Web Controls' started by William Parker, Jun 26, 2004.

  1. I have a user web control I wrote which is registered in my webforms. By
    default the web form's Page_Load is firing first, and then the web user
    control's Page_Load is firing.

    I want to set it up so that the web control's event fires first, before the
    web form's (or any other controls page_load fires). Is this possible?

    Consider this hidden code section (hidden by VS.NET by default) in the web
    control's user form:

    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);
    }

    Would it be possible for me to write code that enumerated all the registered
    events and then rewrote them back in an order to ensure its Page_Load was
    called first? Or perhaps there is an easier way for me to accomplish this?

    Also, as a side note, is the Page_Load event the first event to fire on a
    page? For example I'm looking to do something on the page that happens at
    the very beginning that the page is called. Not at the httpmodule level,
    but ideally there would be no processing prior to the point where I could do
    some things - if Page_Load is not the event is there another one I can
    capture?

    Thanks.
     
    William Parker, Jun 26, 2004
    #1
    1. Advertising

  2. No. The first event is OnInit.
    But using it depends on what you intend to do. Some
    properties/values/references may not yet be set by that time.

    --
    RobertK
    { Clever? No just smart. }


    "William Parker" <> wrote in message
    news:...
    > I have a user web control I wrote which is registered in my webforms. By
    > default the web form's Page_Load is firing first, and then the web user
    > control's Page_Load is firing.
    >
    > I want to set it up so that the web control's event fires first, before

    the
    > web form's (or any other controls page_load fires). Is this possible?
    >
    > Consider this hidden code section (hidden by VS.NET by default) in the web
    > control's user form:
    >
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    > }
    >
    > Would it be possible for me to write code that enumerated all the

    registered
    > events and then rewrote them back in an order to ensure its Page_Load was
    > called first? Or perhaps there is an easier way for me to accomplish

    this?
    >
    > Also, as a side note, is the Page_Load event the first event to fire on a
    > page? For example I'm looking to do something on the page that happens at
    > the very beginning that the page is called. Not at the httpmodule level,
    > but ideally there would be no processing prior to the point where I could

    do
    > some things - if Page_Load is not the event is there another one I can
    > capture?
    >
    > Thanks.
    >
    >
     
    Robert Koritnik, Jun 28, 2004
    #2
    1. Advertising

  3. OnInit would be even better. But I really need a way to organize the events
    so that the OnInit event is fired in my custom control first. Is this
    possible?

    "Robert Koritnik" <> wrote in message
    news:...
    > No. The first event is OnInit.
    > But using it depends on what you intend to do. Some
    > properties/values/references may not yet be set by that time.
    >
    > --
    > RobertK
    > { Clever? No just smart. }
    >
    >
     
    William Parker, Jun 28, 2004
    #3
  4. Why do you need to execute control's events first... could you enlighten us
    a bit? Maybe we would offer a different approach.

    --
    RobertK
    { Clever? No just smart. }

    "William Parker" <> wrote in message
    news:...
    > OnInit would be even better. But I really need a way to organize the

    events
    > so that the OnInit event is fired in my custom control first. Is this
    > possible?
    >
    > "Robert Koritnik" <> wrote in message
    > news:...
    > > No. The first event is OnInit.
    > > But using it depends on what you intend to do. Some
    > > properties/values/references may not yet be set by that time.
    > >
    > > --
    > > RobertK
    > > { Clever? No just smart. }
    > >
    > >

    >
    >
     
    Robert Koritnik, Jun 29, 2004
    #4
  5. I am producing a user control that partner sites will include in all their
    ASP.NET pages. This control is responsible for logging important
    information about the request to a database. It is critical that my control
    log this data before any of their controls or pages begin to execute for
    reasons too complicated to go into. So I want to make certain that my
    OnInit event fires before any others on their page.

    Technically this particular user control does not produce any HTML output.
    This user control actually is a "smart wrapper" of sorts for my class
    library. So instead of giving the third party developers several lines of
    code to set properties and call methods of in my class library, I'm wrapping
    everything that needs to be done into a web control. That way they just
    need to register and include the user control in their page, and the control
    will take care of the more complicated use of the class library for them.
    Is this the right approach to encapsulating complicated calls to a class
    library and make it easy for more novice programmers to call?

    In any event, if there was a clever way for me to ensure my OnInit event
    would fire first that would be great.

    Thank you.

    "Robert Koritnik" <> wrote in message
    news:#...
    > Why do you need to execute control's events first... could you enlighten

    us
    > a bit? Maybe we would offer a different approach.
    >
    > --
    > RobertK
    > { Clever? No just smart. }
    >
    > "William Parker" <> wrote in message
    > news:...
    > > OnInit would be even better. But I really need a way to organize the

    > events
    > > so that the OnInit event is fired in my custom control first. Is this
    > > possible?
    > >
    > > "Robert Koritnik" <> wrote in

    message
    > > news:...
    > > > No. The first event is OnInit.
    > > > But using it depends on what you intend to do. Some
    > > > properties/values/references may not yet be set by that time.
    > > >
    > > > --
    > > > RobertK
    > > > { Clever? No just smart. }
    > > >
    > > >

    > >
    > >

    >
    >
     
    William Parker, Jun 30, 2004
    #5
  6. Normally things are executed this way:
    - constructor: page first then all controls
    - OnInit: controls first, then the page
    - OnLoad: page first, then controls
    - other events...

    Decide for yourself. If you have the data by the time constructor is called
    use that. That's the very first time your control executes any code.

    then there is LoadViewState and so many other methods. Check MSDN what else
    you could use. Check creating server controls articles.

    I suggest you really check some very deep articles, because making good
    server controls shouldn't be held as an easy task eventhough it looks like
    that.

    Check this article on MSDN: "Understanding ASP.NET ViewState" it will
    explain many things about this and maybe you'll get another idea (like
    inheriting a Page class instead of including your own server control on
    every page) I think since your control doesn't have a visualization part I
    would create my own class inherited from Page that would do this. Any child
    private events are FIRST called on the base class, SECOND on the running
    instance itself (ie: Load event).

    --
    RobertK
    { Clever? No just smart. }


    "William Parker" <> wrote in message
    news:...
    > I am producing a user control that partner sites will include in all their
    > ASP.NET pages. This control is responsible for logging important
    > information about the request to a database. It is critical that my

    control
    > log this data before any of their controls or pages begin to execute for
    > reasons too complicated to go into. So I want to make certain that my
    > OnInit event fires before any others on their page.
    >
    > Technically this particular user control does not produce any HTML output.
    > This user control actually is a "smart wrapper" of sorts for my class
    > library. So instead of giving the third party developers several lines of
    > code to set properties and call methods of in my class library, I'm

    wrapping
    > everything that needs to be done into a web control. That way they just
    > need to register and include the user control in their page, and the

    control
    > will take care of the more complicated use of the class library for them.
    > Is this the right approach to encapsulating complicated calls to a class
    > library and make it easy for more novice programmers to call?
    >
    > In any event, if there was a clever way for me to ensure my OnInit event
    > would fire first that would be great.
    >
    > Thank you.
    >
    > "Robert Koritnik" <> wrote in message
    > news:#...
    > > Why do you need to execute control's events first... could you enlighten

    > us
    > > a bit? Maybe we would offer a different approach.
    > >
    > > --
    > > RobertK
    > > { Clever? No just smart. }
    > >
    > > "William Parker" <> wrote in message
    > > news:...
    > > > OnInit would be even better. But I really need a way to organize the

    > > events
    > > > so that the OnInit event is fired in my custom control first. Is this
    > > > possible?
    > > >
    > > > "Robert Koritnik" <> wrote in

    > message
    > > > news:...
    > > > > No. The first event is OnInit.
    > > > > But using it depends on what you intend to do. Some
    > > > > properties/values/references may not yet be set by that time.
    > > > >
    > > > > --
    > > > > RobertK
    > > > > { Clever? No just smart. }
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Robert Koritnik, Jun 30, 2004
    #6
    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. ryu
    Replies:
    4
    Views:
    438
    Rutger Smit
    Sep 1, 2004
  2. Stuart Whiteford
    Replies:
    2
    Views:
    756
    Stuart Whiteford
    Apr 25, 2006
  3. ton

    events won't fire first time

    ton, Nov 5, 2010, in forum: ASP .Net
    Replies:
    0
    Views:
    643
  4. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    407
    Chris
    Aug 30, 2005
  5. Amelyan
    Replies:
    0
    Views:
    208
    Amelyan
    Nov 4, 2005
Loading...

Share This Page