将脚本引用添加到aspx页面. [英] adding script reference to aspx page.

查看:138
本文介绍了将脚本引用添加到aspx页面.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,

我有一个分为两个asp:content的应用程序,我需要向该应用程序添加一个jquery脚本引用,问题是我们需要在头部添加脚本引用,但是在这种情况下,我应该在哪里添加引用?
有人可以建议吗?



I have a application which is divided in to two asp:content and i need to add one jquery script reference to that application, the problem is we need to add the script reference in the head section but in this context where should i add the reference?
can anyone suggest?

推荐答案

将页面引用添加到中央位置(在本例中为母版页")的标题部分始终是一个好主意您可以轻松找到它们的位置.

例如
It''s always a good idea to add your page references in a central place, the Master Page in this case, in the header section where you can easily locate them.

e.g.
<script type="text/javascript" src="/scripts/js-file.js"></script>


理想情况下,您应该在母版页"的页眉"部分中添加对JavaScript/JQuery文件的引用.但是您也可以在内容页面中添加引用,如下所示.
Ideally you should add reference of of your JavaScript/JQuery file in Header section of Master Page. But you can also add reference in content page as below.
<asp:content id="Content1" contentplaceholderid="Test" runat="server" >
    <script type="text/javascript" src="Scripts/MyJavaScript.js"></script>


您好,

我建议您使用页面的ClientScript Manager添加脚本文件:

Hi,

I would suggest that you add the script-file using the page''s ClientScript Manager:

ClientScript.RegisterClientScriptInclude(typeof(TestPage), "jQuery-1.4.3", "/scripts/jquery-1.4.3.js");



这会将脚本放在内容的顶部(在来自ASP.NET的回发脚本之后).另外,如果您有许多需要相同脚本的控件,则可以使用相同的键在每个控件上注册脚本,并且ScriptManager仅将其加载一次.因此,如果脚本已经加载,您就不必摆弄:)

如果您坚持将脚本加载到标头中,则可以通过以下方式进行操作:



This will place the script at the top of your content (after the Postback-Scripts from the ASP.NET). Also if you have many controls which requires the same script, you can register the script on each control with the same key and the ScriptManager will load it only once. So you don''t have to mess around if a script is already loaded :)

If you insist of loading the script in the header, you could do it this way:

Header.Controls.Add(new LiteralControl("<script src=\"/scripts/jquery-1.4.3.js\" type=\"text/javascript\"></script>"));



这要求您的<head>runat="server",否则将不起作用.

希望这会有所帮助.

致以诚挚的问候,祝您编程愉快,
克里斯



This requires that your <head> is runat="server", otherwise this would not work.

Hope this helps.

Best regards and happy coding,
Chris


这篇关于将脚本引用添加到aspx页面.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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