Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function called anytime ANY page is loaded in your application?

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?

like image 259
Hazior Avatar asked Feb 24 '10 14:02

Hazior


People also ask

How do you call a function on page load in Blazor?

The first way, is in your @code block, you can override the OnInitialized method. I use the async version as you can kick off your task and let the page basics load, and then it will refresh when it gets set up.

How call javascript function on page load in MVC?

$(function() { ValidatefuneralDate(); }); this will get invoked when the DOM is ready. $(document). on("pageload",function(){ ValidatefuneralDate(); });

What is the use of page load event in asp net?

The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page. This is the first place in the page lifecycle that all values are restored.


2 Answers

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

like image 73
Patrick Karcher Avatar answered Sep 19 '22 11:09

Patrick Karcher


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

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

like image 34
Fitzchak Yitzchaki Avatar answered Sep 23 '22 11:09

Fitzchak Yitzchaki