13

I want to be able to run a script anytime ANY page is loaded in the application. Is there somewhere I can simply add this? Or do I have to add the code in every page load?

Patrick Karcher
  • 22,995
  • 5
  • 52
  • 66
Hazior
  • 696
  • 4
  • 10
  • 26
  • 5
    Don't forget though, if you use AJAX, there can be requests for things that aren't a page, just as something to consider. – JB King Feb 24 '10 at 14:59

5 Answers5

26

You can do one of three things:

  1. Use a base page in your application, and have all the pages in your application inherit from it. In the page_load event in the base page, do what you have to do. Make sure that the inheriting pages in your app call the base page's page_load event if they override page_load (they usually do). And because page_load is over-used, I'll give the related advice to look at all the page events (especially especially page_prerender) in case another is more appropriate.

  2. Use the events that fire in the global.asax page, which happen whenever a request is received. Check out the Application_BeginRequest event. But, there's a bunch of events there, so check them all out in case another event is more applicable to your situation. (Just like the regular page events, don't get in the bad habit of always using the same event.)

  3. There's a chance that what you want to have happen each time should go into a master page, especially if it's layout related. Master pages seem cutesy but have proved themselves in good designs. If you use a master page's page_load event for common functionality, you don't have to call it from each content page's page_load; it fires every time after the called-page's page_load event. (I mention this because it's easy to confuse master pages and base pages at first.)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Patrick Karcher
  • 22,995
  • 5
  • 52
  • 66
  • I ended up using the master page since it pertained to it. But I was looking into the global.asax prior to asking. Couldn't find it though. Thanks a million. – Hazior Feb 24 '10 at 18:07
  • 1
    Yes, this happens to every one of us. Where is it!?! Though it's a special file that VS knows about, it's not there by default. A .net app doesn't *need* a global.asax. In 2008 go to File, New File, and select the **global application class**. In 2005 I think you do File, New, File, then select it. – Patrick Karcher Feb 24 '10 at 20:07
5

You can use BeginRequest event in the Global.asax file.

http://msdn.microsoft.com/en-us/library/system.web.httpapplication.beginrequest.aspx

Fitzchak Yitzchaki
  • 9,095
  • 12
  • 56
  • 96
4

You could also create and register an HTTP Module. The advantage of that is that they are registered in the web.config, so you can add and remove them at runtime if you want... and have more than one.

Nick
  • 5,875
  • 1
  • 27
  • 38
1

You could create a common base class for your pages, descended from System.Web.UI.Page and add the code in an OnLoad handler there.

Ray
  • 21,485
  • 5
  • 48
  • 64
1

You can use PageAdapters to inject Code on every aspx page request by intercepting any method of ASP.Net Page life cycle.

This article can help you understand its working: http://dotnet.org.za/hiltong/archive/2008/01/06/injecting-a-page-base-class-in-asp-net.aspx

Regards.

Shoaib Shaikh
  • 4,565
  • 1
  • 27
  • 35