在 ASP.NET 页面中包含脚本的正确方法

发布于 2024-09-08 17:09:12 字数 1082 浏览 3 评论 0 原文

我有一个 ASP.NET 应用程序,它混合使用了由不同背景的几位开发人员编写的 ASP.NET AJAX(包括 UpdatePanel)和 jQuery。

有些在 ScriptManager 本身内包含脚本,而其他则使用 Page.ClientScript.RegisterClientScriptInclude后面的代码,其他人直接使用

如果可能的话,我想合并为一种处理此问题的方法,但我不确定每种方法的优缺点是什么以及首选哪种方法。


一个例子是:

protected override void Render(HtmlTextWriter writer)
 {
    Page.ClientScript.RegisterClientScriptInclude("jQuery", ResolveClientUrl("~/scripts/jquery/js/jquery-1.4.2.min.js"));    
    base.Render(writer);
}

vs

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/scripts/jquery/js/jquery-1.4.2.min.js" />
    </Scripts>
</asp:ScriptManager>

vs

<script src="Scripts/jQuery/js/jquery-1.4.2.min.js" type="text/javascript"></script>

I've got an ASP.NET application that uses a mixture of ASP.NET AJAX (Including UpdatePanel) and jQuery written by several developers of differing backgrounds.

Some included scripts within the ScriptManager itself <asp:ScriptManager><Scripts><asp:ScriptReference...., while others used Page.ClientScript.RegisterClientScriptInclude in the code behind, and others used straight <script src=""> to include scripts.

I'd like to consolidate to a single way of handling this if possible, but I'm not sure what the pros/cons of each way are and which way is preferred.


One example would be:

protected override void Render(HtmlTextWriter writer)
 {
    Page.ClientScript.RegisterClientScriptInclude("jQuery", ResolveClientUrl("~/scripts/jquery/js/jquery-1.4.2.min.js"));    
    base.Render(writer);
}

vs

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/scripts/jquery/js/jquery-1.4.2.min.js" />
    </Scripts>
</asp:ScriptManager>

vs

<script src="Scripts/jQuery/js/jquery-1.4.2.min.js" type="text/javascript"></script>

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

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

发布评论

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

评论(3

故事灯 2024-09-15 17:09:12

是调用 ScriptManager.RegisterScriptBlock() 的声明式等价物,就像任何其他 ASP.NET 控件声明都类似于执行编程 Controls.Add()

话虽这么说,确实没有“首选方法”。如果您正在构建可分发的 Web 控件,那么您将需要从控件的设置例程中调用各种 .RegisterScript...() 方法,而不是依赖用户添加标记。

如果您只想在网站中包含脚本,那么使用标记方法可能更方便。

如果您在 Ajax 调用期间添加脚本,那么您可能需要使用 ScriptManager 来完成此操作。如果您要在常规回发中添加脚本,那么您可能需要使用 ClientScriptManager 来执行此操作。

所以......很难列举优点和缺点。就您而言,如果您不构建可再分发的代码,那么最明显、最直接的方法是通过标记包含脚本引用,因为它往往更明显。但我不确定您是否能够将每一次出现的情况合并为一种方法,因为所有方法的存在都是有原因的,并且您可能需要使用多个方法。

An <asp:scriptreference> is the declarative equivalent of calling ScriptManager.RegisterScriptBlock(), much like any other asp.net control declaration is similar to doing a programmatic Controls.Add().

That being said, there really isn't a "preferred way". If you're building distributable web controls, then you'll want to be calling the various .RegisterScript...() methods from your control's setup routines, rather than relying on the user having to add markup.

If you just want to include scripts in a website, it's probably more convenient to use the markup method.

If you're adding scripts during Ajax calls, then you would want to use the ScriptManager to do it. If you're adding scripts on regular postbacks, then you would want to use the ClientScriptManager to do it.

So... it's tough to enumerate the pros and cons. In your case, if you're not building redistributable code, then the most obvious and straightforward way is to include script references via markup, because it tends to be more visible. But I'm not sure you'll be able to consolidate every occurrence to one method, because all the methods exist for a reason, and you may need to use more than one.

原来分手还会想你 2024-09-15 17:09:12

并不总是可以将所有脚本包含在标记中。
如果脚本依赖于动态数据,那么您需要从后面的代码“注入”脚本。

我的规则是尽可能避免从代码后面“注入”脚本,并且只将其作为最后的手段。

编辑:
如果您有许多不同的脚本文件,则首选使用 scriptmanager。
使用脚本管理器时,所有脚本将“连接”到一个文件中。这会影响浏览器下载脚本的方式。浏览器通常限制 2 个并发连接,如果您有 10 个脚本,则需要通过 5 个步骤将它们全部下载。脚本管理器将确保这一切在一个步骤中发生。

It's not always possible to include all script in the markup.
If the script depends on dynamic data then you'll need to "inject" the script from the code behind.

My rule is to always try to avoid "injecting" script from code behind if possible and only use that as a last resort.

Edit:
Using scriptmanager is prefered if you have many different script files.
When using scriptmanager all scripts will be "concatenated" into a single file. This has an impact on how the browser downloads the scripts. Browsers usually have a limit of 2 concurrent connections and if you have 10 scripts then you'll need to download them all in 5 steps. Scriptmanager will see to it that it all happens in a single step.

梦一生花开无言 2024-09-15 17:09:12

I would recommend the Ajax script loader (if you are working in an asp.net, asp.net ajax library, jquery environment)

http://www.asp.net/ajaxlibrary/Ajax%20Script%20Loader.ashx

http://www.asp.net/ajaxlibrary/HOW%20TO%20Combine%20Scripts%20using%20the%20Script%20Loader.ashx

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