简体   繁体   English

在ASP.net页面加载时以编程方式运行javascript的最佳方法是什么?

[英]What is the best way to programmatically run javascript when an ASP.net page loads?

In my global.asax file for my ASP.net project, I am checking for certain conditions. 在我的ASP.net项目的global.asax文件中,我正在检查某些条件。 When those conditions are met, I want to automatically execute javascript code when the page runs. 当满足这些条件时,我想在页面运行时自动执行javascript代码。

This is my code: 这是我的代码:

if condition Then 如果条件然后
Response.Write(" < script type=""text/javascript"" > ") Response.Write(“<script type =”“text / javascript”“>”)
Response.Write(" // Javascript code to do stuff ") Response.Write(“// Javascript代码做东西”)
Response.Write(" < /script > ") Response.Write(“</ script>”)
End If 万一

While this appears to work to execute the Javascript code, I don't think it's a best practice because this code will preceed all of the HTML of the page that gets loaded. 虽然这似乎有助于执行Javascript代码,但我认为这不是最佳实践,因为此代码将在加载的页面的所有 HTML之前。

What is the best way of programmatically tacking on some extra Javascript code to be run when my page loads? 在我的页面加载时,以编程方式添加一些额外的Javascript代码的最佳方法是什么?

Update Thanks for the answer. 更新感谢您的回答。 Too bad this solution doesn't work from within global.asax . 太糟糕了,这个解决方案在global.asax不起作用。 Is there no way to make this happen site-wide? 有没有办法在整个网站范围内实现这一目标? It seems like global.asax would be the logical place to put code that runs with every page... Response.Write works fine in global.asax . 似乎global.asax是放置与每个页面一起运行的代码的合理位置...... Response.Writeglobal.asax工作正常。

To correctly manage the placement of scripts from server controls or pages, you should use ClientScriptManager.RegisterStartupScript() 要从服务器控件或页面正确管理脚本的放置,您应该使用ClientScriptManager.RegisterStartupScript()

You can also use the equivalent method in the ScriptManager if you are using ajax controls in your site: ScriptManager.RegisterStartupScript 如果在站点中使用ajax控件,也可以在ScriptManager中使用等效方法: ScriptManager.RegisterStartupScript

These methods take care of outputting your script in the appropriate locations to be run when the document is finished loading. 这些方法负责在文档加载完成后在适当的位置输出脚本。 The links posted have some good examples to work from. 发布的链接有一些很好的例子可供使用。

Note that you won't be able to use these methods directly from global.asax, as it has no reference to the current page. 请注意,您将无法直接从global.asax使用这些方法,因为它没有引用当前页面。 Global.asax is for hooking events to the HttpApplication pipeline, and is decoupled from the actual HttpHandler such as your Page object. Global.asax用于将事件挂钩到HttpApplication管道,并且与实际的HttpHandler(例如Page对象)分离。 To keep the separation of UI, you'd be better off to do the check in your master page or in some base page class, and output the script from there. 为了保持UI的分离,最好在主页或某些基页类中进行检查,然后从那里输出脚本。

back in the days of asp.net ajax 1.0 we had something called Sys.WebForms.PageRequestManager in the ajax client libraries. 回到asp.net ajax 1.0的时代,我们在ajax客户端库中有一个名为Sys.WebForms.PageRequestManager的东西。 Haven't worked with asp.net 3.5 and asp.net ajax, but I am sure it is simply enough to add the asp.net ajax javascript file into your page and you'll be able to make use of it. 没有使用过asp.net 3.5和asp.net ajax,但我确信它足以将asp.net ajax javascript文件添加到你的页面中,你就可以使用它了。 Check out for an event called pageLoaded event. 检查一个名为pageLoaded事件的事件。

ps: this event will fire every time there is a sync or async postback ps:每次有同步或异步回发时,都会触发此事件

If you want to do it on every page, put it on your masterpage if you have one. 如果您想在每个页面上执行此操作,请将其放在主页上(如果有的话)。

If not, you can create a base Page class, that inherits from System.Web.UI.Page and extend it to check your conditions & render javascript. 如果没有,您可以创建一个基页面类,它继承自System.Web.UI.Page并扩展它以检查您的条件并呈现javascript。

Then in the codebehind of each page, inherit your base Page Class instead of System.Web.UI.Page 然后在每个页面的代码隐藏中,继承您的基本页面类而不是System.Web.UI.Page

public partial class Orders : MyCode.BasePage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM