在您的应用程序中加载任何页面时是否有一个函数被调用?

发布于 2024-08-22 08:42:54 字数 67 浏览 8 评论 0 原文

我希望能够在应用程序中加载任何页面时运行脚本。有什么地方我可以简单地添加这个吗?或者我是否必须在每个页面加载中添加代码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月亮是我掰弯的 2024-08-29 08:42:54

您可以执行以下三件事之一:

  1. 使用应用程序中的基页,并让应用程序中的所有页面都继承自它。在基页的page_load事件中,做你必须做的事情。确保应用程序中的继承页面覆盖时调用基页面的 page_load 事件page_load (他们通常这样做)。并且由于 page_load 被过度使用,我将提供相关建议以查看 所有页面事件(特别是page_prerender)以防另一个更合适。

  2. 使用global.asax 页面,每当收到请求时就会发生。查看 Application_BeginRequest 事件。但是,那里有很多活动,所以 全部检查,以防其他活动更适合您的情况。 (就像常规页面事件一样,不要养成总是使用相同事件的坏习惯。)

  3. 有可能每次您希望发生的事情都应该放入 母版页,尤其是如果它与布局相关。母版页看起来很可爱,但已经在良好的设计中证明了自己。如果您将母版页的 page_load 事件用于常用功能,则不必从每个内容页的 page_load 调用它;它每次都会触发 在被调用页面的 page_load 事件之后。 (我提到这一点是因为一开始很容易混淆母版页和基本页。)

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

莫言歌 2024-08-29 08:42:54

您可以在 Global.asax 文件中使用 BeginRequest 事件。

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

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

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

画▽骨i 2024-08-29 08:42:54

您还可以创建并注册 HTTP 模块 。这样做的优点是它们在 web.config 中注册,因此如果您愿意,您可以在运行时添加和删除它们......并且有多个。

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.

清欢 2024-08-29 08:42:54

您可以为您的页面创建一个公共基类(继承自 System.Web.UI.Page),并在其中的 OnLoad 处理程序中添加代码。

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.

演出会有结束 2024-08-29 08:42:54

您可以使用 PageAdapters 通过拦截 ASP.Net 页面生命周期的任何方法来在每个 aspx 页面请求上注入代码。

这篇文章可以帮助您了解其工作原理:
http://dotnet.org.za/hiltong/archive/2008/01/06/injecting-a-page-base-class-in-asp-net.aspx

问候。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文