正确的方法,包括在一个asp.net页面脚本 [英] Proper way to include scripts in an asp.net page

查看:148
本文介绍了正确的方法,包括在一个asp.net页面脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了使用由不同背景的几个开发人员编写ASP.NET AJAX(含的UpdatePanel)和jQuery的混合物的ASP.NET应用程序。

部分包括使用ScriptManager本身的脚本< ASP:ScriptManager的><脚本>< ASP:的ScriptReference .... ,而使用其他 Page.ClientScript.RegisterClientScriptInclude 在code的后面,和其他人使用的直< SCRIPT SRC => 包括脚本。

我想巩固的可能,如果这种处理方式单一,但我不知道有什么利弊/每一种方式缺点是,哪些方法是preferred。


一个例子是:

 保护覆盖无效渲染(HtmlTextWriter的作家)
 {
    Page.ClientScript.RegisterClientScriptInclude(jQuery的,ResolveClientUrl(〜/脚本/ jQuery的/ JS / jQuery的-1.4.2.min.js));
    base.Render(作家);
}

VS

 < ASP:的ScriptManager ID =ScriptManager1=服务器>
    <脚本和GT;
        < ASP:的ScriptReference PATH =〜/脚本/ jQuery的/ JS / jQuery的-1.4.2.min.js/>
    < /脚本>
< / ASP:ScriptManager的>

VS

 <脚本SRC =脚本/ jQuery的/ JS / jQuery的-1.4.2.min.js类型=文/ JavaScript的>< / SCRIPT>


解决方案

这并不总是可能包括在标记的所有脚本。结果
如果脚本依赖于动态数据,那么你就需要从后面code注入的剧本。

我的规则是总是尽量避免从code注射的剧本后面如果可能的话,只把它作为最后的手段。

编辑:结果
使用ScriptManager的是,如果你有很多不同的脚本文件prefered。结果
当使用ScriptManager的所有脚本将串联到一个文件中。这对浏览器如何将下载的脚本产生影响。浏览器通常有2个并发连接的限制,如果你有10个脚本,那么你需要下载他们所有的5个步骤。 ScriptManager可以看到它,这一切发生在一个单一的步骤。

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>

解决方案

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.

这篇关于正确的方法,包括在一个asp.net页面脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆