Поделиться через


How to: Determine Whether ASP.NET AJAX Functionality is Available

ASP.NET AJAX features require that the System.Web.Extensions assembly be installed in the global assembly cache (GAC) of the server where the Web application is hosted. If the assembly is available, your application and any associated controls in the App_Code folder or in an assembly the Bin folder can use AJAX features. For example, they can register scripts or create UpdatePanel controls.

This topic describes two tasks:

  • How to use reflection to check for the System.Web.Extensions assembly. This check is useful when you create custom controls that work regardless of whether ASP.NET AJAX is installed.

  • How to use the GetCurrent method of the ScriptManager class to determine whether AJAX features are enabled on an individual page. This is useful because scripts that are used in asynchronous postbacks must be registered with the ScriptManager control. Otherwise, the scripts can be registered with the ClientScriptManager control.

For more information about AJAX functionality in ASP.NET, see UpdatePanel Control Overview and Partial-Page Rendering Overview.

To use reflection to determine whether ASP.NET AJAX is installed

  • Use the following code to determine whether ASP.NET AJAX is installed in the global assembly cache.

    Private Shared ReadOnly ReflectionLock As Object = New Object()
        SyncLock ReflectionLock
          Dim scriptManagerType = _
            Type.GetType( _
            "System.Web.UI.ScriptManager, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35", _
             False)
          If Not (scriptManagerType Is Nothing) Then
            ' ASP.NET AJAX is installed.
          Else
            ' ASP.NET AJAX is not installed.
          End If
        End SyncLock
    
    Private static readonly object ReflectionLock = new object();
    Lock (ReflectionLock)
    {
        Type scriptManagerType =
           Type.GetType(
           "System.Web.UI.ScriptManager, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35",
           false);
        if (scriptManagerType != null)
            // ASP.NET AJAX is installed.
        else
            // ASP.NET AJAX is not installed.
    

    }

    Note

    Make sure that you set the Version attribute to the correct version number of the assembly that you are checking for.

Checking Whether AJAX Functionality is Enabled for a Page

Even if ASP.NET AJAX functionality is enabled for a Web application, you might have to check whether AJAX functionality is enabled for an individual page.

To determine whether AJAX functionality is enabled for a Web page

  • Use the following code to determine whether AJAX functionality is enabled for a page.

    Dim sm as ScriptManager = ScriptManager.GetCurrent(Page)
    If (sm is Nothing)
        ' ASP.NET AJAX functionality is not enabled for the page.
    Else
        ' ASP.NET AJAX functionality is enabled for the page.
    End If
    
    ScriptManager sm = ScriptManager.GetCurrent(Page)
    if (sm == null)
    {
        // ASP.NET AJAX functionality is not enabled for the page.
    }
    else
    {
        // AJAX functionality is enabled for the page.
    }
    

    Note

    To determine whether partial page rendering is supported for a page, you can modify this code to use the EnablePartialRendering and the SupportsPartialRendering property of the ScriptManager control.

See Also

Concepts

ASP.NET AJAX Overview