How to include jQuery in ASP.net page?(如何在 ASP.net 页面中包含 jQuery?)
问题描述
我有一个 ASP.net UserControl,它要求包含页面包含对 jquery 的引用.
i have an ASP.net UserControl
that requires the containing page to include a reference to jquery.
在过去,我只会在包含页面中包含对 jQuery 的引用:
In the olden days, i would simply have included a reference to jQuery in the containing page:
<HEAD>
<SCRIPT type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></SCRIPT>
</HEAD>
但是我的 UserControl 对 jQuery 的依赖是一个内部实现细节,不应该泄露到外部.我的 userControl 如何指示 jQuery 包含在最终页面中?
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?
研究这个,我发现了很多令人困惑的解决方案,在不同的时间调用不同的函数.我不愿提及其中的任何,因为人们可能认为其中任何一个都是有效的.我希望得到正确的答案,而不是有效的答案.
Researching this, i find a lot of confused solutions, calling different functions at different times. i hesitate to mention any of them, because people might think that any of them are valid. i am hoping for the correct answer, not an answer that works.
不同的解决方案涉及调用:
Different solutions involve calling:
- 调用
this.Page.ClientScript.RegisterClientScriptInclude
在Render
期间 - 调用
this.Page.ClientScript.RegisterStartupScript
在Render
期间 - 调用
this.Page.ClientScript.RegisterStartupScript
在Page_Load
期间 - 调用
this.Page.ClientScript.RegisterStartupScript
按钮点击 - 调用
Page.Header.Controls.Add(new LiteralControl({ Text = "<script type="text/javascript" src="..."></script>";);
李> - 只需在您的
userControl.ascx
文件 - 在
RegisterClientScriptBlock
期间调用code>OnPreRender
- calling
this.Page.ClientScript.RegisterClientScriptInclude
duringRender
- calling
this.Page.ClientScript.RegisterStartupScript
duringRender
- calling
this.Page.ClientScript.RegisterStartupScript
duringPage_Load
- calling
this.Page.ClientScript.RegisterStartupScript
during a button click - calling
Page.Header.Controls.Add(new LiteralControl( { Text = "<script type="text/javascript" src="..."></script>";);
- simply including a
<SCRIPT>
element in youruserControl.ascx
file - calling
RegisterClientScriptBlock
duringOnPreRender
我的困惑集中在:
- 我什么时候想使用
RegisterClientScriptInclude
与RegisterStartupScript
? - 我什么时候想在
Page_Load
vsRender
vsPreRender
vs 点击按钮期间调用它? - 我如何为
RegisterXxxxScriptXxx
提供"Scripts/jquery-1.7.2.min.js"
的路径?
- when would i want to use
RegisterClientScriptInclude
vsRegisterStartupScript
? - when would i want to call it during
Page_Load
vsRender
vsPreRender
vs a button click? - how do i give
RegisterXxxxScriptXxx
the path to"Scripts/jquery-1.7.2.min.js"
?
短版:我如何转换
<%@ 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>
用于用户控件:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MySuperCoolControl.ascx.cs" Inherits="Controls_MySuperCoolControl" %>
推荐答案
你可以使用google托管的jquery,如下:
You can use google hosted jquery as follows:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
这篇关于如何在 ASP.net 页面中包含 jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 ASP.net 页面中包含 jQuery?
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01