ASP.Net: execute function on every page loaded?

  • Thread starter Thread starter JackBlack
  • Start date Start date
J

JackBlack

Hi, all! Using ASP.Net (2.0 framework) with VB.Net code-behind...

Is there any way to execute a particular piece of code on each and every
page load in a website, without specifically putting that same code (or user
control) on each and every page? Some languages have a config file (like
application.cfm for Cold Fusion) that loads on every page where one can run
basic code on each page load, without adding code to each page.

Is there such a beastie with ASP.Net? I know that global.asax won't do the
trick...

Thanks for any suggestions!
Jack
 
Global.Asax do have a beginrrequest event ? You could also inherit from a
base page ? An httpModule ?

As John said, it's often more efficient to explain what you want to do. It
allows peers to give better advices (and perhaps sometimes a new approach to
the same problem).
 
JackBlack said:
Hi, all! Using ASP.Net (2.0 framework) with VB.Net code-behind...

Is there any way to execute a particular piece of code on each and every
page load in a website, without specifically putting that same code (or user
control) on each and every page?

Sure. You'll probably want to derive your own custom Page object, and
have all your pages use that as their base class.

So, step one is to create a new class called MyPage or whatever, based
on System.Web.UI.Page. Override the Page_Load method there to run your
common code, or create your own protected Initialize method that you'll
call from every page that needs it.

Next, base your codebehind classes off of MyPage rather than
System.Web.UI.Page. Problem solved.

Good luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
Back
Top