ScriptReference 類別

定義

註冊 ECMAScript (JavaScript) 檔案以用於 ASP.NET 網頁。

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
繼承
ScriptReference

範例

下列範例示範如何參考自訂控制項和內嵌在控制群組件中的 JavaScript 檔案。 元件假設位於網站的 Bin 資料夾中。 自訂控制項會以動畫顯示 UpdatePanel 控制項。 JavaScript 檔案會編譯為名為 SampleControl.UpdatePanelAnimation.js 的內嵌資源。 您可以使用 和 Name 屬性來註冊內嵌的 JavaScript 檔案 Assembly

若要使用此範例,請使用自訂控制項,將範例中顯示的 JavaScript 檔案編譯為內嵌資源。 將產生的元件放入網站的 Bin 資料夾中。 如需如何在元件中內嵌 JavaScript 檔案的範例,請參閱 逐步解說:將 JavaScript 檔案內嵌為元件中的資源

下列範例顯示使用自訂控制項的頁面。

<%@ 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>

下列範例顯示自訂控制項類別定義。

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 檔案。

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 檔案的程式碼。

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

備註

您可以透過 ScriptReference 物件註冊 JavaScript 檔案,在 ASP.NET 網頁上加入 JavaScript 檔案。 您可以將位於 .js 檔案的腳本檔案註冊 (網站上的靜態腳本檔案) 。 您也可以註冊內嵌為元件中資源的腳本檔案。 註冊腳本檔案之後,您可以在網頁上的用戶端腳本中使用其函式。

若要註冊靜態腳本檔案,請將 Path 物件的 屬性 ScriptReference 設定為檔案的相對位置。

若要註冊內嵌為元件中資源的腳本檔案,請將 Assembly 屬性設定為包含該檔案的元件名稱。 然後將 屬性設定 Name 為內嵌在元件中的.js檔名稱。 在此情況下,腳本檔案必須內嵌,而不是連結。

您可以設定 ScriptMode 屬性,指出要使用腳本的偵錯或發行版本本。

Auto 會根據它參考獨立腳本檔案或內嵌為元件中資源的腳本檔案,產生不同的結果。 獨立腳本檔案是使用 Path 屬性來定義。 必須透過 NameAssembly 屬性存取元件參考。 值的結果 Auto 如下所示:

  • 當它套用至指定 屬性的獨立腳本檔案 Path 時, Auto 值相當於 Release

  • 當它套用至元件中的腳本參考時, Auto 相當於 Inherit 。 僅 Name 指定 時,會使用它來參考腳本。 同時指定 和 Path 屬性時 NamePath 會使用 屬性,而不是 Name ,但 Auto 值仍然等於 Inherit

建構函式

ScriptReference()

初始化 ScriptReference 類別的新執行個體。

ScriptReference(String)

使用指定的路徑來初始化 ScriptReference 類別的新執行個體。

ScriptReference(String, String)

使用指定的名稱和組件來初始化 ScriptReference 類別的新執行個體。

屬性

Assembly

取得或設定組件的名稱,這個組件包含用戶端指令碼檔做為內嵌資源。

IgnoreScriptPath
已淘汰.

取得或設定值,這個值指出當您註冊資源中的用戶端指令碼檔時,ScriptPath 屬性是否包含在 URL 中。

Name

取得或設定內嵌資源的名稱,這個資源包含用戶端指令碼檔。

NotifyScriptLoaded
已淘汰.

取得或設定值,指出 ScriptResourceHandler 物件是否會自動在 ECMAScript (JavaScript) 檔案結尾加上程式碼,以呼叫 Sys.Application 類別的用戶端 NotifyScriptLoaded 方法。

(繼承來源 ScriptReferenceBase)
Path

取得或設定參考的用戶端指令碼檔路徑,這個路徑相對於網頁。

(繼承來源 ScriptReferenceBase)
ResourceUICultures

取得或設定以逗號分隔的 UI 文化特性清單,這些文化特性是由 Path 屬性支援。

(繼承來源 ScriptReferenceBase)
ScriptMode

取得或設定要使用的用戶端指令碼檔之版本 (發行版本或偵錯版本)。

(繼承來源 ScriptReferenceBase)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
GetUrl(ScriptManager, Boolean)

擷取會轉譯成 src 項目之 script 屬性值的 URL。

IsAjaxFrameworkScript(ScriptManager)

判斷指令碼參考是否為 AJAX 指令碼。

IsAjaxFrameworkScript(ScriptManager)

判斷指定的指令碼參考是否為 ASP.NET AJAX 指令碼。

(繼承來源 ScriptReferenceBase)
IsFromSystemWebExtensions()
已淘汰.

指出複合指令碼是否要包含對 ASP.NET AJAX 架構指令碼的參考。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回字串,表示 Name 屬性、Path 屬性或型別名稱的值。

適用於