Can web control events fire FIRST?

W

William Parker

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

Robert Koritnik

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

William Parker

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?
 
R

Robert Koritnik

Why do you need to execute control's events first... could you enlighten us
a bit? Maybe we would offer a different approach.
 
W

William Parker

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

Robert Koritnik

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

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top