Using the ClientScriptManager Class


Using the ClientScriptManager Class

The ClientScriptManager class contains the main application programming interface for working with JavaScript. You'll make heavy use of this class whenever you add JavaScript to your custom controls.

The ClientScriptManager class supports the following methods for adding JavaScript to a page:

  • RegisterArrayDeclaration Enables you to add a JavaScript array to a page.

  • RegisterClientScriptBlock Enables you to add a JavaScript script after the page's opening server-side <form> tag.

  • RegisterClientScriptInclude Enables you to add a JavaScript include after the page's opening server-side <form> tag.

  • RegisterClientScriptResource Enables you to add JavaScript in a page that has been compiled into an assembly.

  • RegisterExpandoAttribute Enables you to add script for adding an expando attribute to an element of the page.

  • RegisterHiddenField Enables you to add a hidden form field after the page's opening server-side <form> tag.

  • RegisterOnSubmitStatement Enables you to add JavaScript that executes immediately before a page is posted back to the server.

  • RegisterStartupScript Enables you to add a JavaScript script before the page's closing server-side <form> tag.

Notice that there are two methods for rendering a JavaScript script in the body of a page: RegisterClientScriptBlock() and RegisterStartupScript(). The only difference between these methods is the location where they render the JavaScript. The location of a JavaScript script in a page matters because you cannot refer to an HTML element in JavaScript unless the script is located after the element. If you use the RegisterStartupScript() method, then you know that all the HTML elements in the body of the server-side <form> tag have been created.

All the methods listed here were designed so that you can safely call them more than once. Because you might have multiple instances of the same control in the same page, you don't want to add duplicate instances of the same script to a page. For example, if you call the RegisterClientScriptInclude() method more than once, then only one JavaScript include is added to the page.

You can detect whether or not a script has already been registered in a page by using one of the following methods:

  • IsClientScriptBlockRegistered Returns true when a script has already been registered with the RegisterClientScriptBlock() method.

  • IsClientScriptIncludeRegistered Returns true when a JavaScript include has already been registered with the RegisterClientScriptInclude() method.

  • IsOnSubmitStatementRegistered Returns TRue when a script has already been registered with the RegisterOnSubmitStatement() method.

  • IsStartupScriptRegistered Returns TRue when a script has already been registered with the RegisterStartupScript() method.

Detecting Browser Capabilities

After you have entered the messy universe of JavaScript, you must handle the frustrating incompatibilities between different web browsers. For example, you don't want to call the showModalDialog() or addEventListener() method on a browser that doesn't support it. You can detect browser capabilities either on the client side or the server side.

On the client side, you can perform feature detection in your JavaScript scripts to check whether particular methods are supported by a browser. For example, Internet Explorer and Firefox use different methods for adding an event handler. Internet Explorer uses the attachEvent() method and Firefox uses the (more standards-compliant) addEventListener() method.

The following script correctly adds a load event handler in the case of both browsers:

if (window.addEventListener)    window.addEventListener('load', doSomething, false); else    window.attachEvent('onload', doSomething); 


When you request a page that contains this script with Internet Explorer, calling window.addEventListener returns a value equivalent to false and the window.attachEvent() method is used. When you request a page that contains this script with Firefox or Opera, on the other hand, the window.addEventListener() method is called.

On the server side, you can use the properties of the HttpBrowserCapabilities class to detect the features of the browser being used to request a page. This class has a huge number of properties (too many to list here). However, here are some of the more useful properties that you can detect:

  • ActiveXControls Returns true when a browser supports ActiveXControls.

  • AOL Returns true when a browser is an American Online browser.

  • Browser Returns the type of browser (for example, IE, Firefox, Opera).

  • ClrVersion Returns the latest version of the .NET Framework installed on the browser.

  • Cookies Returns true when a browser supports cookies.

  • EcmaScriptVersion Returns the version of JavaScript supported by the browser.

  • MajorVersion Returns the major version of the browser as an Integer.

  • MinorVersion Returns the minor version of the browser as a couble.

  • MinorVersionString Returns the minor version of the browser as a string.

  • MSDomVersion Returns the version of the Microsoft Document Object Model supported by the browser.

  • Platform Returns the platform of the client (for example, WinXP).

  • SupportsCallback Returns true when a browser supports AJAX.

  • SupportsCSS Returns true when a browser supports Cascading Style Sheets.

  • Version Returns the full version of the browser.

  • W3CDomVersion Returns the W3C Document Object Model version supported by the browser (for example, 1.0).

The HttpBrowserCapabilities object is exposed through the Request object. You use Request.Browser to get a reference to the HttpBrowserCapabilities object. For example, you can use the following code to execute a subroutine only when the requesting browser is Internet Explorer version 5.0 or greater:

If Request.Browser.Browser = "IE" And Request.Browser.MajorVersion >= 5 Then     doSomething() End If 


Behind the scenes, the HttpBrowserCapabilities object uses the User-Agent header sent by a browser to determine the browser's capabilities. A database of browser capabilities is stored in a set of XML files located in the following folder:

\WINDOWS\Microsoft.NET\Framework\[version]\CONFIG\Browsers 


The information reported back by these properties is only as accurate as the information stored in these XML files.




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net