Client-Side Scripting in ASP.NET 2.0


I've heard that client-side scripting has been more tightly integrated into ASP.NET 2.0. Can you provide an overview of these improvements?

Microsoft has reorganized and enhanced ASP.NET client-side scripting with the release of Windows .NET Framework. 2.0. In Framework 1.1, the methods of the page object (the object representing the current Web page) provide some support for client-side scripting. Although Framework 2.0 includes these methods for backward compatibility, Microsoft encourages people to embrace the new but similar methods of the ClientScriptManager object, which has 36 methods in all. I'm going to briefly explore and provide examples for the following six methods, which you can use to support client-side scripting in your Web pages:

  • RegisterStartupScript and its sidekick IsStartupScriptRegistered
  • RegisterClientScriptBlock and its sidekick IsClientScriptBlockRegistered
  • RegisterClientScriptInclude and its sidekick IsClientScriptIncludeRegistered

Figure 1 provides an overview of how the main methods of RegisterStartupScript, RegisterClientScriptBlock, and RegisterClientScriptInclude fit into an ASP.NET 2.0 Web page. Although not pictured, the sidekick methods help these main functions by detecting whether a particular script has been registered. Figure 1 also shows how the client-side script gets sent, or emitted, to a client's Web page. Typically, the script is written in Java-Script because most browsers natively support this scripting language. On the Windows Scripting Solutions Web site, you'll find the code for all six methods in the sample ASP.NET 2.0 page named Default.aspx and a sample client-side script named CheckIEBrowser.js Go to, enter 49418 in the InstantDoc ID text box, then click the hotlink.

When you use any of these six methods, you must have a client-side script that you would like to emit to the page. Besides having that script, your accompanying code must follow a common pattern. First, the code needs to create an instance of the ClientScriptManager object when the page loads during the Page_Load event. Second, the code needs to define a unique key that identifies the particular script you're writing into the page. This key is important because ClientScriptManager uses it in certain methods to ensure that a script loads only once in a page. Finally, the code should use a method called GetType to get a Type object that represents an instance of the current object and its associated metadata. Using this method is important when you're writing custom controls that are included in a page, but it doesn't hurt to use it even when everything is contained in the page code. In that situation, Get-Type simply returns the current page's Type object. The code in Listing 4 shows the pattern I just described. This .NET code is written in C#. Go to if you'd like to see Visual Basic (VB) code samples.

Now that you know about the prerequisites for using the six methods, let's look how to use each one, starting with RegisterStartupScript. The RegisterStartupScript method emits a client-side script to a page each time the page loads. ASP.NET 1.1 has a simpler version of this method, but the simpler version doesn't contain the Type declaration parameter or the Boolean parameter called adScriptTags. When you set addScriptTags to True, RegisterStartupScript automatically adds opening and closing script tags and comment tags to your code, as the following shows:

<script type="text/javascript"> 
<!--code goes here--> 

This feature will simplify your code slightly.

IsStartupScriptRegistered, a new method related to RegisterStartupScript, ensures that the startup code isn't written to the page more than once. Listing 5 shows code that uses both the IsStartupScriptRegistered and RegisterStartupScript methods in an ASP.NET 2.0 page. IsStartupScriptRegistered checks whether a script that has the value in the startupScriptKey variable appears somewhere in the specified page output. If not, IsStartupScriptRegistered loads the script. This check avoids the possibility of some other control loading the same script to the page.

The script, which appears in callout A, makes up the bulk of Listing 5. The script uses the StringBuilder object to create a JavaScript string. After callout A, the RegisterStartupScript method registers this script so that it runs when the page loads. The HTML portion of the page contains two input buttons, a table, and a p element. The startup script writes the text that Figure 2 shows to the p element when the page loads. The p element contains the id=information attribute and value. Other scripts on the page use the button and table elements.

The RegisterClientScriptBlock method lets you write an event-driven block of code. Although the RegisterStartupScript method loads when the Page_Load event occurs, a script registered to the RegisterClientScriptBlock method can respond to many events, such as button clicks or mouseovers. Like RegisterStartupScript, RegisterClientScriptBlock has a simpler counterpart in ASP.NET 1.1 and adds two new parameters in ASP.NET 2.0: Type and addScriptTags.

ASP.NET 2.0 contains a new method called IsClientScriptBlockRegistered, which works the same way as IsStartupScriptRegistered. As Listing 6 shows, IsClientScriptBlockRegistered and RegisterClientScriptBlock work together. Callout A in Listing 6 highlights the script, which uses the StringBuilder object to create a JavaScript string. Notice that this script contains a function called IEVersion. In Default.aspx, this function name is assigned to a button's onclick event. Clicking the button runs the script in Listing 6 and displays the output that appears in Figure 3. Listing 6's script uses the Dynamic HTML (DHTML) window.navigator object to return information about the current browser. In the code at callout A in Listing 6, note the call to the ClearTable function. This function's code doesn't appear in Listing 6 but rather in the CheckIEBrowser.js file included in the page that uses the RegisterClientScriptInclude method.

The RegisterClientScriptInclude method and its related IsClientScriptIncludeRegistered method are new to Framework 2.0. The handy RegisterClientScriptInclude method lets you include a JavaScript file in an ASP.NET 2.0 page without having to write the JavaScript include directive (<script type="text/javascript" src=" script_name.js"> </script>) by using a string or the StringBuilder object as you might have done in previous versions of the Framework. Similar to the other methods I just covered, the IsClientScriptIncludeRegistered method helps ensure that the Java-Script file isn't included in the page more than once.

Listing 7 demonstrates how to use the IsClientScriptIncludeRegistered method to include a script named Check-IEBrowser.js in the page. The Check-IEBrowser.js file contains two functions: ClearTable, which the script block in Listing 6 uses, and MsIeVersionInfo, which writes out current browser information in a table format, as Figure 4 shows. The include-script approach is powerful because it reduces the amount of code that appears in the script element of the ASP.NET page or in a separate ASP .NET file. In addition, the approach provides a framework for building a centralized repository of common, reusable script functions. Functions in the included script are accessible by any other script in the ASP.NET page. For example, the MsIeVersionInfo function calls the ClearTable function, as does the IEVersion function inside the script block in the ASP.NET page. Although the ClientScriptManager object has 36 methods, you'll find that the six I covered here are an excellent starting point because you can often use them to enhance your ASP.NET 2.0 Web pages with client-side JavaScript scripts. If there is reader interest, I'm more than happy to explore the other methods of this powerful client-side script enabler. Please let Windows Scripting Solutions know of your interest by sending email to [email protected]

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.