Documentation for: ASP.NET Ajax Version 1.0

This documentation is for a previous version. For the current released version, see the ASP.NET Ajax documentation on MSDN.

 

 

 

 

 

 

 

Skip Navigation Links.
Tutorials
Sample ASP.NET AJAX Application
ASP.NET AJAX and JavaScript
Extending JavaScript with ASP.NET AJAX
Creating Custom Client Script in ASP.NET AJAX
Dynamically Assigning ASP.NET AJAX Script References
Globalizing a Date by Using Client Script
Embedding a JavaScript File as a Resource in an Assembly
Embedding Localized Resources for a JavaScript File
Adding Localized Resources to a JavaScript File
Creating Custom Client Events
The UpdatePanel Control
Introduction to the UpdatePanel Control
Creating a Simple ASP.NET Page with Multiple UpdatePanel Controls
Using the UpdatePanel Control with Data-Bound Controls
Using the UpdatePanel Control with Master Pages
Using the UpdatePanel Control with User Controls
Using the UpdatePanel Control with a Web Service
Customizing Error Handling for UpdatePanel Controls
Animating UpdatePanel Controls
Canceling an Asynchronous Postback
Giving Precedence to a Specific Asynchronous Postback
Working with PageRequestManager Events
The UpdateProgress Control
Introduction to the UpdateProgress Control
Programming UpdateProgress Controls in Client Script
The Timer Control
Introduction to the Timer Control
Using the Timer Control with Multiple UpdatePanel Controls
ASP.NET Application Services
Using Forms Authentication
Using Profile Information
Web Services
Exposing Web Services to Client Script
Calling Web Services from Client Script
ASP.NET AJAX Extensibility
Creating Custom ASP.NET AJAX Non-Visual Client Components
Creating Custom ASP.NET AJAX Client Controls
Creating an Extender Control
Adding Client Capabilities to a Web Server Control
Creating a Client Component Class Using the Prototype Model
Defining Custom Component Properties and Raising PropertyChanged Events
Releasing Component Resources

Dynamically Assigning ASP.NET AJAX Script References

Introduction

In most scenarios, the easiest way to add a script file to an ASP.NET page is in markup, as in the following example:

<asp:ScriptManager ID="SMgr" runat="server">
  <Scripts>
    <asp:ScriptReference Path="./Script.js" />
  </Scripts>
</asp:ScriptManager> 

However, it is also possible to add script references dynamically. Page developers can do this to have more control over how a script is added. For example, they might add scripts dynamically to help conserve memory resources by not loading a large script library unless it is needed. Or they might load different versions of scripts for different types of users. Control developers add scripts dynamically when they create script controls or extender controls in order to make script resources automatically available to the page that hosts the control.

This topic addresses a simple page developer scenario. For adding script references in custom controls, see Adding Client Behaviors to Web Server Controls by Using ASP.NET AJAX Extensions.

Script references can specify script files or scripts embedded as resources in assemblies. Scripts can exist in debug and retail versions. The following procedure shows how to assign a script reference to a page in each of these situations.

note

All script files to be registered with the ScriptManager control must call the notifyScriptLoaded method to notify the application that the script has finished loading. Assembly-based scripts should not call this method in most cases. For more information, see notifyScriptLoaded Method.

To dynamically add a script reference to a page

  1. If you do not know the ID of the <asp:ScriptManager> element on the page, call the GetCurrent() method of the ScriptManager control to get the current instance of the control. Test for null in case there is no ScriptManager control on the page. If you know that there is an <asp:ScriptManager> element on the page and you know its ID value, you can skip this step.

    The following example shows how to test for the existence of a ScriptManager control on a page, and then either get the current instance or create a new instance.

    CS

    // If there is a ScriptManager on the page, use it.
    // If not, throw an exception.
    ScriptManager Smgr = ScriptManager.GetCurrent(Page);
    if (Smgr == null) throw new Exception("ScriptManager not found.");
    
    

    VB

    ' If there is a ScriptManager on the page, use it.
    ' If not, throw an exception.
    Dim SMgr As ScriptManager
    If ScriptManager.GetCurrent(Page) Is Nothing Then
        Throw New Exception("ScriptManager not found.")
    Else : SMgr = ScriptManager.GetCurrent(Page)
    End If
    
    
  2. Create a ScriptReference object.

    CS

    ScriptReference SRef = new ScriptReference();
    
    

    VB

    Dim SRef As ScriptReference
    SRef = New ScriptReference()
    
    
  3. For file-based scripts, if you know that the ScriptPath property of the ScriptManager control is set to the correct location for the script file, set the Name property of the ScriptReference instance to the name of the script file. Otherwise, set the Path property of the ScriptReference object to the absolute, relative, or application-relative URL of the script file to add.

    CS

    // If you know that Smgr.ScriptPath is correct...
    SRef.Name = "Script.js";
    
    // Or, to specify an app-relative path...
    SRef.Path = "~/Scripts/Script.js";
    
    

    VB

    ' If you know that Smgr.ScriptPath is correct...
    SRef.Name = "Script.js"
    
    ' Or, to specify an app-relative path...
    SRef.Path = "~/Scripts/Script.js"
    
    
  4. If the script is part of an assembly, set the Name and Assembly properties of the ScriptReference instance.

    CS

    SRef.Name = "Script.js";
    SRef.Assembly = "ScriptAssembly";
    
    

    VB

    SRef.Name = "Script.js"
    SRef.Assembly = "ScriptAssembly"
    
    
  5. Specify whether to run debug or release versions of the script. To set this mode for all scripts on the page, set the ScriptMode property of the ScriptManager control. To set debug mode for an individual script, set the ScriptMode property of the ScriptReference object.

    The following example demonstrates both options.

    CS

    // To set ScriptMode for all scripts on the page...
    Smgr.ScriptMode = ScriptMode.Release;
    
    //Or, to set the ScriptMode just for the one script...
    SRef.ScriptMode = ScriptMode.Debug;
    
    //If they conflict, the setting on the ScriptReference wins.
    
    

    VB

    ' To set ScriptMode for all scripts on the page...
    SMgr.ScriptMode = ScriptMode.Release
    
    'Or, set ScriptMode for just for the one script...
    SRef.ScriptMode = ScriptMode.Debug
    
    'If they conflict, the setting on the ScriptReference wins.
    
    
    note

    If the Path property of the ScriptReference object is not set, the ScriptMode property of the ScriptManager control is set to Release by default. If the Path property of the ScriptReference object is set, the ScriptManager control looks for both debug and release scripts unless its ScriptMode property is set to a specific mode.

  6. Add the ScriptReference object to the Scripts collection of the ScriptManager control.

    CS

    Smgr.Scripts.Add(SRef);
    
    

    VB

    SMgr.Scripts.Add(SRef)