ScriptReference Class

Definition

注册 ECMAScript (JavaScript) 文件,以便在 ASP.NET 网页上使用。Registers an ECMAScript (JavaScript) file for use on an ASP.NET Web page.

public ref class ScriptReference : System::Web::UI::ScriptReferenceBase
public class ScriptReference : System.Web.UI.ScriptReferenceBase
type ScriptReference = class
    inherit ScriptReferenceBase
Public Class ScriptReference
Inherits ScriptReferenceBase
Inheritance
ScriptReference

Examples

下面的示例演示如何引用嵌入到控件程序集中的自定义控件和 JavaScript 文件。The following example shows how to reference a custom control and a JavaScript file that is embedded in the control assembly. 该程序集假定在网站的 Bin 文件夹中。The assembly is assumed to be in the Bin folder of the Web site. 自定义控件将动画处理 UpdatePanel 控件。The custom control animates UpdatePanel controls. JavaScript 文件编译为名为 SampleControl 的嵌入资源。The JavaScript file is compiled as an embedded resource that is named SampleControl.UpdatePanelAnimation.js. 使用 "Assembly" 和 "Name" 属性注册嵌入的 JavaScript 文件。You register the embedded JavaScript file by using the Assembly and Name properties.

若要使用此示例,请使用自定义控件,将示例中显示的 JavaScript 文件编译为嵌入的资源。To use this example, compile the JavaScript file that is shown in the example as an embedded resource with the custom control. 将生成的程序集放入网站的 Bin 文件夹中。Put the resulting assembly into the Bin folder of the Web site. 有关如何在程序集中嵌入 JavaScript 文件的示例,请参阅演练:将 Javascript 文件作为资源嵌入到程序集中For an example of how to embed a JavaScript file in an assembly, see Walkthrough: Embedding a JavaScript File as a Resource in an Assembly.

下面的示例演示使用自定义控件的页。The following example shows a page that uses the custom control.

<%@ Page Language="C#" %>
<%@ Register TagPrefix="Samples" Namespace="SampleControl" Assembly="SampleControl" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ScriptReference</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" 
                                 EnablePartialRendering="True"
                                 runat="server">
             <Scripts>
                <asp:ScriptReference Assembly="SampleControl" Name="SampleControl.UpdatePanelAnimation.js" />
             </Scripts>
            </asp:ScriptManager>
            
                       
            <Samples:UpdatePanelAnimationWithClientResource 
                     ID="UpdatePanelAnimator1"
                     BorderColor="Green"
                     Animate="true"
                     UpdatePanelID="UpdatePanel1"
                     runat="server" >
            </Samples:UpdatePanelAnimationWithClientResource>
            <asp:UpdatePanel ID="UpdatePanel1" 
                               UpdateMode="Conditional"
                               runat="server">
                <ContentTemplate>
                    <asp:Calendar ID="Calendar2" 
                                  runat="server">
                    </asp:Calendar>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="true" %>

<%@ Register TagPrefix="Samples" Namespace="SampleControl" Assembly="SampleControl" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ScriptReference</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" 
                                 EnablePartialRendering="True"
                                 runat="server">
             <Scripts>
                <asp:ScriptReference Assembly="SampleControl" Name="SampleControl.UpdatePanelAnimation.js" />
             </Scripts>
            </asp:ScriptManager>
            
                       
            <Samples:UpdatePanelAnimationWithClientResource 
                     ID="UpdatePanelAnimator1"
                     BorderColor="Green"
                     Animate="true"
                     UpdatePanelID="UpdatePanel1"
                     runat="server" >
            </Samples:UpdatePanelAnimationWithClientResource>
            <asp:UpdatePanel ID="UpdatePanel1" 
                               UpdateMode="Conditional"
                               runat="server">
                <ContentTemplate>
                    <asp:Calendar ID="Calendar2" 
                                  runat="server">
                    </asp:Calendar>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>

下面的示例演示了自定义控件类定义。The following example shows the custom control class definition.

using System;
using System.Drawing;
using System.Web.UI;
using System.Web;
using System.Globalization;

namespace SampleControl
{
    public class UpdatePanelAnimationWithClientResource : Control
    {
        private string _updatePanelID;
        private Color _borderColor;
        private Boolean _animate;
        public Color BorderColor
        {
            get
            {
                return _borderColor;
            }
            set
            {
                _borderColor = value;
            }
        }

        public string UpdatePanelID
        {
            get
            {
                return _updatePanelID;
            }
            set
            {
                _updatePanelID = value;
            }
        }

        public Boolean Animate
        {
            get
            {
                return _animate;
            }
            set
            {
                _animate = value;
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (Animate)
            {

                UpdatePanel updatePanel = (UpdatePanel)FindControl(UpdatePanelID);

                string script = String.Format(
                   CultureInfo.InvariantCulture,
                   @"
Sys.Application.add_load(function(sender, args) {{
var {0}_borderAnimation = new BorderAnimation('{1}');
var panelElement = document.getElementById('{0}');
     if (args.get_isPartialLoad()) {{
        {0}_borderAnimation.animate(panelElement);
    }}
}})
",
                   updatePanel.ClientID,
                   ColorTranslator.ToHtml(BorderColor));

                ScriptManager.RegisterStartupScript(
                    this,
                    typeof(UpdatePanelAnimationWithClientResource),
                    ClientID,
                    script,
                    true);
            }
        }
    }
}
Imports System.Web.UI
Imports System.Drawing
Imports System.Globalization

Public Class UpdatePanelAnimationWithClientResource
    Inherits Control

    Private _updatePanelID As String
    Private _borderColor As Color
    Private _animate As Boolean

    Public Property BorderColor() As Color
        Get
            Return _borderColor
        End Get
        Set(ByVal value As Color)
            _borderColor = value
        End Set
    End Property

    Public Property UpdatePanelID() As String
        Get
            Return _updatePanelID
        End Get
        Set(ByVal value As String)
            _updatePanelID = value
        End Set
    End Property

    Public Property Animate() As Boolean
        Get
            Return _animate
        End Get
        Set(ByVal value As Boolean)
            _animate = value
        End Set
    End Property

    Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
        MyBase.OnPreRender(e)
        If (Animate) Then

            Dim updatePanel As UpdatePanel = CType(Me.FindControl(UpdatePanelID), UpdatePanel)

            Dim script As String = String.Format( _
                   CultureInfo.InvariantCulture, _
                   "Sys.Application.add_load(function(sender, args) {{var {0}_borderAnimation = new BorderAnimation('{1}');var panelElement = document.getElementById('{0}');if (args.get_isPartialLoad()) {{{0}_borderAnimation.animate(panelElement);}}}});", _
                   updatePanel.ClientID, _
                   ColorTranslator.ToHtml(BorderColor))


            ScriptManager.RegisterStartupScript( _
                Me, _
                GetType(UpdatePanelAnimationWithClientResource), _
                ClientID, _
                script, _
                True)
        End If
    End Sub
End Class

下面的示例演示支持 JavaScript 文件。The following example shows the supporting JavaScript file.

BorderAnimation = function(color) {
    this._color = color;
}

BorderAnimation.prototype = {
    animate: function(panelElement) {
        var s = panelElement.style;
        s.borderWidth = '2px';
        s.borderColor = this._color;
        s.borderStyle = 'solid';

        window.setTimeout(
            function() {{
                s.borderWidth = 0;
            }},
            500);
    }
}
        

下面的示例演示必须添加到包含自定义控件和 JavaScript 文件的项目的 AssemblyInfo 文件中的代码。The following example shows code that you must add to the AssemblyInfo file of the project that contains the custom control and the JavaScript file.

[assembly: System.Web.UI.WebResource("SampleControl.UpdatePanelAnimation.js", "application/x-javascript")]
<Assembly: System.Web.UI.WebResource("SampleControl.UpdatePanelAnimation.js", "application/x-javascript")> 

Remarks

可以通过 ScriptReference 对象注册 JavaScript 文件,将该文件包含在 ASP.NET 网页上。You can include a JavaScript file on an ASP.NET Web page by registering it through a ScriptReference object. 你可以在网站上注册一个作为 .js 文件(静态脚本文件)的脚本文件。You can register a script file that is located as a .js file (a static script file) on the Web site. 你还可以将嵌入的脚本文件注册为程序集中的资源。You can also register a script file that is embedded as a resource in an assembly. 注册脚本文件后,可以在网页上的客户端脚本中使用其功能。After registering the script file, you can use its functions in client script on the Web page.

若要注册静态脚本文件,请将 ScriptReference 对象的 Path 属性设置为该文件的相对位置。To register a static script file, set the Path property of the ScriptReference object to the relative location of the file.

若要注册嵌入为程序集中的某个资源的脚本文件,请将 Assembly 属性设置为包含该文件的程序集的名称。To register a script file that is embedded as a resource in an assembly, set the Assembly property to the name of the assembly that contains the file. 然后,将 Name 属性设置为嵌入到程序集中的 .js 文件的名称。Then set the Name property to the name of the .js file that is embedded in the assembly. 在这种情况下,必须嵌入脚本文件,而不是链接。In that case, the script file must be embedded, not linked.

设置 ScriptMode 属性,以指示是使用脚本的调试版本还是发布版本。You set the ScriptMode property to indicate whether to use the debug or release version of the script.

Auto 值将产生不同的结果,具体取决于它是引用独立脚本文件还是引用嵌入为程序集中的资源的脚本文件。The Auto value produces different results depending on whether it refers to a standalone script file or to a script file that is embedded as a resource in an assembly. 独立脚本文件由 Path 属性定义。A standalone script file is defined with the Path property. 必须通过 NameAssembly 属性访问程序集引用。An assembly reference must be accessed through the Name and Assembly properties. Auto 值的结果如下所示:The results for the Auto value are as follows:

  • 当应用于指定 Path 属性的独立脚本文件时,Auto 值等效于 ReleaseWhen it is applied to a standalone script file where the Path property is specified, the Auto value is equivalent to Release.

  • 当应用于程序集中的脚本引用时,Auto 等效于 InheritWhen it is applied to a script reference in an assembly, Auto is equivalent to Inherit. 仅指定 Name 时,它用于引用脚本。When only Name is specified, it is used to reference the script. 当同时指定 NamePath 属性时,将使用 Path 属性,而不是 Name,但 Auto 值仍等效于 InheritWhen Name and the Path property are both specified, the Path property is used instead of Name, but the Auto value is still equivalent to Inherit.

Constructors

ScriptReference()

初始化 ScriptReference 类的新实例。Initializes a new instance of the ScriptReference class.

ScriptReference(String)

使用指定的路径初始化 ScriptReference 类的新实例。Initializes a new instance of the ScriptReference class by using a specified path.

ScriptReference(String, String)

使用指定的名称和程序集初始化 ScriptReference 类的新实例。Initializes a new instance of the ScriptReference class by using a specified name and assembly.

Properties

Assembly

获取或设置包含作为嵌入资源的客户端脚本文件的程序集名称。Gets or sets the name of the assembly that contains the client script file as an embedded resource.

IgnoreScriptPath

获取或设置一个值,该值指示从资源注册客户端脚本文件时,URL 中是否包括 ScriptPath 属性。Gets or sets a value that indicates whether the ScriptPath property is included in the URL when you register a client script file from a resource.

Name

获取或设置包含客户端脚本文件的嵌入资源的名称。Gets or sets the name of the embedded resource that contains the client script file.

NotifyScriptLoaded

获取或设置一个值,该值指示 ScriptResourceHandler 对象是否在 ECMAScript (JavaScript) 文件末尾自动添加代码来调用 NotifyScriptLoadedSys.Application 类的客户端 方法。Gets or sets a value that indicates whether the ScriptResourceHandler object automatically adds code at the end of the ECMAScript (JavaScript) file to call the client NotifyScriptLoaded method of the Sys.Application class.

(Inherited from ScriptReferenceBase)
Path

获取或设置引用客户端脚本文件相对于网页的路径。Gets or sets the path of the referenced client script file, relative to the Web page.

(Inherited from ScriptReferenceBase)
ResourceUICultures

获取或设置 Path 属性支持的 UI 区域性的逗号分隔列表。Gets or sets a comma-delimited list of UI cultures that are supported by the Path property.

(Inherited from ScriptReferenceBase)
ScriptMode

获取或设置要使用的客户端脚本文件的版本(发布版本或调试版本)。Gets or sets the version of the client script file (release or debug) to use.

(Inherited from ScriptReferenceBase)

Methods

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
GetUrl(ScriptManager, Boolean)

检索呈现为 src 元素的 script 特性值的 URL。Retrieves the URL that is rendered as the value of the src attribute of the script element.

IsAjaxFrameworkScript(ScriptManager)

确定脚本引用是否为 AJAX 脚本。Determines whether the script reference is an AJAX script.

IsAjaxFrameworkScript(ScriptManager)

确定指定的脚本引用是否为 ASP.NET AJAX 脚本。Determines whether the specified script reference is an ASP.NET AJAX script.

(Inherited from ScriptReferenceBase)
IsFromSystemWebExtensions()

指示复合脚本是否包含对 ASP.NET AJAX 框架脚本的引用。Indicates whether the composite script contains a reference to an ASP.NET AJAX framework script.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

返回一个字符串,该字符串表示 Name 属性、Path 属性或类型名称的值。Returns a string that represents the value of the Name property, the Path property, or the type name.

Applies to