Export (0) Print
Expand All

ScriptReference Class

Registers an ECMAScript (JavaScript) file for use on an ASP.NET Web page.

Namespace:  System.Web.UI
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

public class ScriptReference : ScriptReferenceBase

The ScriptReference type exposes the following members.

  NameDescription
Public methodScriptReference()Initializes a new instance of the ScriptReference class.
Public methodScriptReference(String)Initializes a new instance of the ScriptReference class by using a specified path.
Public methodScriptReference(String, String)Initializes a new instance of the ScriptReference class by using a specified name and assembly.
Top

  NameDescription
Public propertyAssemblyGets or sets the name of the assembly that contains the client script file as an embedded resource.
Public propertyIgnoreScriptPath Obsolete. 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.
Public propertyNameGets or sets the name of the embedded resource that contains the client script file.
Public propertyNotifyScriptLoaded Obsolete. 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.)
Public propertyPathGets or sets the path of the referenced client script file, relative to the Web page. (Inherited from ScriptReferenceBase.)
Public propertyResourceUICulturesGets or sets a comma-delimited list of UI cultures that are supported by the Path property. (Inherited from ScriptReferenceBase.)
Public propertyScriptModeGets or sets the version of the client script file (release or debug) to use. (Inherited from ScriptReferenceBase.)
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodGetUrlInfrastructure. Retrieves the URL that is rendered as the value of the src attribute of the script element. (Overrides ScriptReferenceBase.GetUrl(ScriptManager, Boolean).)
Protected methodIsAjaxFrameworkScriptDetermines whether the script reference is an AJAX script. (Overrides ScriptReferenceBase.IsAjaxFrameworkScript(ScriptManager).)
Protected methodIsFromSystemWebExtensionsInfrastructure. Obsolete. Indicates whether the composite script contains a reference to an ASP.NET AJAX framework script. (Overrides ScriptReferenceBase.IsFromSystemWebExtensions().)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the value of the Name property, the Path property, or the type name. (Overrides Object.ToString().)
Top

You can include a JavaScript file on an ASP.NET Web page by registering it through a ScriptReference object. 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.

To register a static script file, set the Path property of the ScriptReference object to the relative location of the file.

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. 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.

You set the ScriptMode property to indicate whether to use the debug or release version of the script.

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. A standalone script file is defined with the Path property. An assembly reference must be accessed through the Name and Assembly properties. The results for the Auto value are as follows:

  • When it is applied to a standalone script file where the Path property is specified, the Auto value is equivalent to Release.

  • When it is applied to a script reference in an assembly, Auto is equivalent to Inherit. When only Name is specified, it is used to reference the script. When 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.

The following example shows how to reference a custom control and a JavaScript file that is embedded in the control assembly. The assembly is assumed to be in the Bin folder of the Web site. The custom control animates UpdatePanel controls. The JavaScript file is compiled as an embedded resource that is named SampleControl.UpdatePanelAnimation.js. You register the embedded JavaScript file by using the Assembly and Name properties.

To use this example, compile the JavaScript file that is shown in the example as an embedded resource with the custom control. Put the resulting assembly into the Bin folder of the Web site. 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>

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);
            }
        }
    }
}

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);
    }
}
        

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")]

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft