Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


March 13, 2006

Client-Side Scripting in ASP.NET 2.0

RSS
View this exclusive article with VIP access -- click here to join |
See More Scripting Articles Here | Reprints | Or sign up for our VIP Monthly Pass!
Main Article    Rem

Download the Code Here

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:

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

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.

   Previous  [1]  2  Next 


Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

How can I stop and start services from the command line?

...

Where is Microsoft NetMeeting in Windows XP?

...


.NET Whitepapers Batch Job Scheduling and .NET in 2008

Related Events Top 11 Reasons Why Oracle Database 11g on Windows is Right For You

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing