ScriptReference クラス

定義

ASP.NET Web ページで使用する ECMAScript (JavaScript) ファイルを登録します。

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 ファイルを参照する方法を示しています。 アセンブリは、Web サイトの Bin フォルダー内にあると見なされます。 カスタム コントロールは、コントロールを UpdatePanel アニメーション化します。 JavaScript ファイルは、SampleControl.UpdatePanelAnimation.jsという名前の埋め込みリソースとしてコンパイルされます。 埋め込み JavaScript ファイルは、プロパティをAssemblyName使用して登録します。

この例を使用するには、カスタム コントロールを使用して埋め込みリソースとして例に示されている JavaScript ファイルをコンパイルします。 結果のアセンブリを Web サイトの 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")>

注釈

JavaScript ファイルは、オブジェクトを介して登録することで、ASP.NET Web ページにScriptReference含めることができます。 Web サイト上の.js ファイル (静的スクリプト ファイル) として配置されているスクリプト ファイルを登録できます。 アセンブリにリソースとして埋め込まれたスクリプト ファイルを登録することもできます。 スクリプト ファイルを登録したら、Web ページのクライアント スクリプトでその関数を使用できます。

静的スクリプト ファイルを登録するには、オブジェクトの Path プロパティを ScriptReference ファイルの相対位置に設定します。

アセンブリにリソースとして埋め込まれたスクリプト ファイルを登録するには、プロパティを Assembly ファイルを含むアセンブリの名前に設定します。 次に、 Name アセンブリに埋め込まれている.js ファイルの名前にプロパティを設定します。 その場合、スクリプト ファイルはリンクされずに埋め込まれている必要があります。

スクリプトの ScriptMode デバッグ バージョンとリリース バージョンのどちらを使用するかを示すプロパティを設定します。

この値は Auto 、スタンドアロン スクリプト ファイルを参照しているか、アセンブリ内のリソースとして埋め込まれているスクリプト ファイルを参照しているかに応じて、異なる結果を生成します。 スタンドアロン スクリプト ファイルは、このプロパティを使用して Path 定義されます。 アセンブリ参照には、プロパティAssemblyを使用してアクセスするName必要があります。 値の Auto 結果は次のとおりです。

  • プロパティが指定されているスタンドアロン スクリプト ファイル Path に適用される場合、 Auto 値は Release.

  • アセンブリ内のスクリプト参照に適用される場合はInheritAuto . 指定された場合にのみ Name 、スクリプトを参照するために使用されます。 プロパティがPath両方とも指定されている場合NamePathプロパティは代わりにName使用されますがAuto、値は引き続き .Inherit

コンストラクター

ScriptReference()

ScriptReference クラスの新しいインスタンスを初期化します。

ScriptReference(String)

指定されたパスを使用して、ScriptReference クラスの新しいインスタンスを初期化します。

ScriptReference(String, String)

指定された名前とアセンブリを使用して、ScriptReference クラスの新しいインスタンスを初期化します。

プロパティ

Assembly

クライアント スクリプト ファイルがリソースとして埋め込まれているアセンブリの名前を取得または設定します。

IgnoreScriptPath
互換性のために残されています。

リソースからクライアント スクリプト ファイルを登録する場合に、URL に ScriptPath プロパティが含まれるかどうかを示す値を取得または設定します。

Name

クライアント スクリプト ファイルが含まれる埋め込みリソースの名前を取得または設定します。

NotifyScriptLoaded
互換性のために残されています。

ECMAScript (JavaScript) ファイルの末尾に、Sys.Application クラスのクライアント NotifyScriptLoaded メソッドを呼び出すコードを ScriptResourceHandler オブジェクトが自動的に追加するかどうかを示す値を取得または設定します。

(継承元 ScriptReferenceBase)
Path

参照するクライアント スクリプト ファイルのパスを、Web ページからの相対的なパスとして取得または設定します。

(継承元 ScriptReferenceBase)
ResourceUICultures

Path プロパティによってサポートされる UI カルチャのコンマ区切りリストを取得または設定します。

(継承元 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 プロパティ、または型名の値を表す文字列を返します。

適用対象