如何jQuery的包括在ASP.net页面? [英] How to include jQuery in ASP.net page?
问题描述
我有一个ASP.net 用户控件
需要包含页,包括jQuery的一个参考。
在古时,我也只会包含在包含页面将jQuery的引用:
< HEAD>
<脚本类型=文/ JavaScript的SRC =脚本/ jQuery的-1.7.2.min.js>< / SCRIPT>
< / HEAD>
不过,我的用户的jQuery的依赖是一个内部实现细节,不应泄漏到外部。我的用户如何决定了jQuery的被包含在最终的页面?
研究这个,我找了很多困惑的解决方案,在不同的时间调用不同的功能。我毫不犹豫地提它们中的任何后,因为人们可能会认为,任何人都有效。我希望为正确答案,而不是一个的作品答案。
不同的解决方案包括呼叫:
- 调用
this.Page.ClientScript.RegisterClientScriptInclude
在渲染
- calling
this.Page.ClientScript.RegisterStartupScript
在渲染
- 调用
this.Page.ClientScript.RegisterStartupScript
在的Page_Load
- 调用
this.Page.ClientScript.RegisterStartupScript
期间按一下按钮 - calling
Page.Header.Controls.Add(新LiteralControl({文字=<脚本类型= \\文/ JavaScript的\\SRC = \\\\>< / SCRIPT>中;);
- 只需包括
< SCRIPT>在
元素的userControl.ascx
文件 - calling
的RegisterClientScriptBlock
在在preRender
我的困惑是围绕中心:
- 时,我会想使用
RegisterClientScriptInclude
VS的RegisterStartupScript
? - 时,我会想在
的Page_Load
来调用它VS渲染
VSpreRender
VS点击一个按钮? - 如何给
RegisterXxxxScriptXxx
路径脚本/ jQuery的-1.7.2.min.js
?
短版:如何转换
<%@页标题=主页LANGUAGE =C#AutoEventWireup =真codeFILE =Default.aspx.cs
继承=_默认%GT;<!DOCTYPE HTML>
< HTML和GT;
<头=服务器>
<脚本类型=文/ JavaScript的SRC =脚本/ jQuery的-1.7.2.min.js>< / SCRIPT>
在一个用户控件使用:
<%@控制语言=C#AutoEventWireup =真codeFILE =MySuperCoolControl.ascx.cs继承=Controls_MySuperCoolControl%GT;
您可以使用谷歌托管的jQuery如下:
<脚本的src =// ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js类型=文/ JavaScript的&GT ;< / SCRIPT>
i have an ASP.net UserControl
that requires the containing page to include a reference to 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>
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:
- 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
My confusion is centered around:
- 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"
?
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" %>
You can use google hosting jquery as follows:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
这篇关于如何jQuery的包括在ASP.net页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!