次の方法で共有


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 ファイルを登録するには、 Assembly プロパティと Name プロパティを使用します。

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

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

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

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

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

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

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

コンストラクター

名前 説明
ScriptReference()

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

ScriptReference(String, String)

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

ScriptReference(String)

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

プロパティ

名前 説明
Assembly

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

IgnoreScriptPath
古い.

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

Name

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

NotifyScriptLoaded
古い.

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

(継承元 ScriptReferenceBase)
Path

Web ページを基準とした、参照先のクライアント スクリプト ファイルのパスを取得または設定します。

(継承元 ScriptReferenceBase)
ResourceUICultures

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

(継承元 ScriptReferenceBase)
ScriptMode

使用するクライアント スクリプト ファイル (リリースまたはデバッグ) のバージョンを取得または設定します。

(継承元 ScriptReferenceBase)

メソッド

名前 説明
Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
GetUrl(ScriptManager, Boolean)

script要素のsrc属性の値としてレンダリングされる URL を取得します。

IsAjaxFrameworkScript(ScriptManager)

スクリプト参照が AJAX スクリプトであるかどうかを判断します。

IsFromSystemWebExtensions()
古い.

複合スクリプトに ASP.NET AJAX フレームワーク スクリプトへの参照が含まれているかどうかを示します。

MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
ToString()

Name プロパティ、Path プロパティ、または型名の値を表す文字列を返します。

適用対象