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 http://www.windowsitpro.com/windowsscripting, enter 49418 in the InstantDoc ID text box, then click the 49418.zip 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 http://msdn2.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx 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:
<!--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 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.