如何在 ASP.net 页面中包含 jQuery?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10786976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:44:57  来源:igfitidea点击:

How to include jQuery in ASP.net page?

jqueryasp.netuser-controls

提问by Ian Boyd

i have an ASP.net UserControlthat requires the containing page to include a reference to jquery.

我有一个 ASP.net UserControl,它要求包含页面包含对 jquery 的引用。

In the olden days, i would simply have included a reference to jQuery in the containing page:

在过去,我只会在包含页面中包含对 jQuery 的引用:

<HEAD>
   <SCRIPT type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></SCRIPT>
</HEAD>

But my UserControl's dependency on jQuery is an internal implementation detail, that should not be leaking to the outside. How can my userControl dictate that jQuery be included in the final page?

但是我的 UserControl 对 jQuery 的依赖是一个内部实现细节,不应该泄漏到外部。我的 userControl 如何决定将 jQuery 包含在最终页面中?



Researching this, i find a lot of confused solutions, calling different functions at different times. i hesitate to mention anyof them, because people might think that any of them are valid. i am hoping for the correctanswer, not an answer that works.

研究这个,我发现了很多混乱的解决方案,在不同的时间调用不同的函数。我不愿提及其中任何一个,因为人们可能认为其中任何一个都是有效的。我希望得到正确的答案,而不是有效的答案。

Different solutions involve calling:

不同的解决方案涉及调用:

My confusion is centered around:

我的困惑集中在:

  • when would i want to use RegisterClientScriptIncludevs RegisterStartupScript?
  • when would i want to call it during Page_Loadvs Rendervs PreRendervs a button click?
  • how do i give RegisterXxxxScriptXxxthe path to "Scripts/jquery-1.7.2.min.js"?
  • 我什么时候想使用RegisterClientScriptIncludevs RegisterStartupScript
  • 我什么时候想在Page_Loadvs Rendervs PreRendervs 按钮点击期间调用它?
  • 我如何给出RegisterXxxxScriptXxx路径"Scripts/jquery-1.7.2.min.js"


Short version: How do i convert

简短版本:我如何转换

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="_Default" %><!DOCTYPE html>
<html>
<head runat="server">
    <script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script>

for use in a UserControl:

用于用户控件:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MySuperCoolControl.ascx.cs" Inherits="Controls_MySuperCoolControl" %>

回答by Waqas Anwar

You can use google hosted jquery as follows:

您可以按如下方式使用谷歌托管的 jquery:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>

回答by Echilon

You can use a ScriptManagerProxyon the UserControl and a ScriptManageron the parent or master page.

您可以ScriptManagerProxy在 UserControl 上使用 a ,ScriptManager在父页面或母版页上使用 a 。

See How Do You Use ScriptManagerProxy In a custom ASP.NET Control

查看如何在自定义 ASP.NET 控件中使用 ScriptManagerProxy

This would take care of "Giving RegisterXxxxScriptXxx the path to "Scripts/jquery-1.7.2.min.js" and remove the need to worry about it during the Page_Load/Page_PreRender events.

这将处理“为 RegisterXxxxScriptXxx 提供“Scripts/jquery-1.7.2.min.js”的路径,并消除在 Page_Load/Page_PreRender 事件期间担心它的需要。

As for "When would i want to use RegisterClientScriptInclude vs RegisterStartupScript?"

至于“我什么时候想使用 RegisterClientScriptInclude 和 RegisterStartupScript?”

RegisterClientScriptIncluderegisters an external JS file to be included in the page. RegisterStartupScriptincludes a block of inline executable script in the page, which is not in an external file.

RegisterClientScriptInclude注册要包含在页面中的外部 JS 文件。RegisterStartupScript在页面中包含一个内联可执行脚本块,该脚本不在外部文件中。

回答by paulH

[this is only relevant if your user control is to be used in-house. If it's for distribution then it won't be of much help]

[这仅在您的用户控件要在内部使用时才相关。如果是为了分发,那么它不会有太大帮助]

Take a look at this link:

看看这个链接:

http://www.codeproject.com/Articles/196727/Managing-Your-JavaScript-Library-in-ASP-NET

http://www.codeproject.com/Articles/196727/Managing-Your-JavaScript-Library-in-ASP-NET

The article suggests creating methods to generate references to javascript libraries such as jQuery, so that if you want to use it in a page you simply call JavascriptLoader.IncludeJQuery() [or whatever you have called your method].

这篇文章建议创建方法来生成对 javascript 库(如 jQuery)的引用,这样如果您想在页面中使用它,您只需调用 JavascriptLoader.IncludeJQuery() [或您调用的任何方法]。

Now what I have done is to take it a step further by creating those methods in an assembly that I have placed in the GAC so that it is available to all my .net web applications. Now, wherever I want to use jQuery, that method is already available. The best thing being that if I call the method in a user control, and call it again in another user control, and again on the page, it still only registers the library once. If I decide to upgrade to a newer version of jQuery, I just change my dll, and it's changed everywhere.

现在我所做的是更进一步,通过在我放置在 GAC 中的程序集中创建这些方法,以便它可用于我的所有 .net Web 应用程序。现在,无论我想在何处使用 jQuery,该方法都已经可用。最好的事情是,如果我在用户控件中调用该方法,并在另一个用户控件中再次调用它,然后在页面上再次调用它,它仍然只注册一次库。如果我决定升级到较新版本的 jQuery,我只需更改我的 dll,并且它在任何地方都已更改。

回答by user1429080

If your control is referenced from a separate project, you can embed the javascript in the other assembly. See this old but good example by Scott Mitchell:

如果您的控件是从一个单独的项目中引用的,您可以将 javascript 嵌入到另一个程序集中。参见 Scott Mitchell 的这个古老但很好的例子:

http://www.4guysfromrolla.com/articles/080906-1.aspx

http://www.4guysfromrolla.com/articles/080906-1.aspx