Autotmate Page_load events

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

We have a web site which we need to implement auditing. I've implemented a
base form which all code behind files inherit from, and I can add the logic
to it, but that would mean that all existing pages would need to be changed,
and any future pages would need to also implement the call code.

Is there a way to automatically call some code when a page_load has been
invoked without adding code into the Page_Load? Global.asa would be nice, but
I didn't find it usefull at a page level.
 
Alex:

Your base form could also hook up to the Page_Load event, and both the
derived form and base form could have an event handler.
 
Can you provide with a little more detail on how I can achieve it. Maybe some
additional keywords I can search on?I don't understand how I can achieve this.
 
Hi Alex:

I was thinking of pointing you to these articles:

http://aspnet.4guysfromrolla.com/articles/041305-1.aspx
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/BedrockASPNET.asp

but it sounds as if you already have a base class that all your
webforms derive from. In that base class you can hook the Load event
just like the derived webform does that VS.NET gives you (in C#, you
would += the Page.Load event). It's ok to have two event handlers
wired up to the same event.

Does that make sense?
 
Yes, thanks. I added this in InitializeComponent : this.Load += new
System.EventHandler(base.Page_Load);

The nice thing about this solution is that it's more scaleable since I can
keep on adding code in the base form if needed. The only downside is that we
need to remember to add the line in all of our forms, but I guess that's what
code reviews are for....
 
Ah, yes, but - you can add that line in the base form. Just override
OnInit and add the event handler for page load. OnInit is called by
the derived web form in the templated code VS.NET ceates for a new web
form.

With that in place you wouldn't need to remember to do anything in the
derived class.
 
Back
Top