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 winscriptsol@windowsitpro.com.