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

How to include jQuery in ASP.net page?(如何在 ASP.net 页面中包含 jQuery?)

本文介绍了如何在 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.RegisterClientScriptIncludeRender
  • 期间
  • 调用this.Page.ClientScript.RegisterStartupScriptRender
  • 期间
  • 调用 this.Page.ClientScript.RegisterStartupScriptPage_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 during Render
  • calling this.Page.ClientScript.RegisterStartupScript during Render
  • calling this.Page.ClientScript.RegisterStartupScript during Page_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 your userControl.ascx file
  • calling RegisterClientScriptBlock during OnPreRender

我的困惑集中在:

  • 我什么时候想使用 RegisterClientScriptIncludeRegisterStartupScript?
  • 我什么时候想在 Page_Load vs Render vs PreRender vs 点击按钮期间调用它?
  • 我如何为 RegisterXxxxScriptXxx 提供 "Scripts/jquery-1.7.2.min.js" 的路径?
  • when would i want to use RegisterClientScriptInclude vs RegisterStartupScript?
  • when would i want to call it during Page_Load vs Render vs PreRender 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?

基础教程推荐