Scott on Writing

Musings on technical writing...

Enhanced Client-Side Script Features in ASP.NET 2.0

There are two flavors of client-side script:

  • Functions and code that runs immediately - such code is placed within <script> blocks on a page.
  • Code that runs in response to some event - this code can be wired up to an HTML element's event handler (i.e., <input type=“button“ onclick=“alert('foo');“ ... />) or specified in code, like window.onload = somefunction.

In ASP.NET 1.x there exist a handful of methods for emiting script in <script> blocks: RegisterClientScriptBlock(), RegisterStartupScript(), RegisterArrayDeclaration(), and so on.  For tying script to an HTML element's event handler, Web controls expose an Attributes collection for this purpose (myButton.Attributes[“onclick“] = “alert('foo');“;, for example).  For much more on this topic check out my article Working with Client-Side Script.

I recently started digging into ASP.NET 2.0 in earnest, as I blogged about before.  The ASP.NET team has added some great improvements to the client-side scripting capabilities of ASP.NET in ASP.NET 2.0.  For one, they moved the RegisterXXX() methods out of the Page class and into a ClientScriptManager class, with the RegisterXXX() methods being static.  This ClientScriptManager class is exposed as an instance through the Page class's ClientScript property, meaning your ASP.NET code now looks like:

ClientScript.RegisterClientScriptBlock(...);

As opposed to:

Page.RegisterClientScriptBlock(...);

(Although the old syntax will work for backwards compatibility, but VS 2005 will give you a warning...)

The method signature of the RegisterXXX() methods have changed, too.  In ASP.NET 1.x they took in two string parameters, a key and the actual script to emit.  In ASP.NET 2.0 they also take in a Type instance, which in most cases will just be set to the type of the control injecting the script.  This allows for two different controls to inject script blocks with the same key without overriding one another's scripts.  There's also an optional final Boolean parameter that, if set to True, will automatically add the opening and closing <script> block tags.

In addition to the ASP.NET 1.x RegisterXXX() methods, 2.0 introduces a couple new ones to boot.  There's the RegisterClientScriptInclude(url) method that adds a <script src=”url” type=”text/javascript” /> tag.  (The benefit of includes being that they reduce the total weight of the page since browsers can cache the external JavaScript file...)  Want something to happen when the form submits?  Use the RegisterOnSubmitStatement() method.

A number of Web controls have also received some script-related upgrades.  The Button/LinkButton/ImageButton controls now have a OnClientClick property, which is basically a shortcut for Attributes[”onclick”], allowing you to do:

myButton.OnClientClick = “alert('foo');”;

All controls also have a Focus() method that injects a bit of script to give focus to the control on document load.

The coolest script-related feature in ASP.NET 2.0, though, is script callback.  With script callback you can have a user action trigger script that makes an HTTP request back to the Web server, invoking a particular method.  The callback method on the server runs and returns a string, which gets passed to a specified JavaScript function.  The end result is that you can update the page in some manner with data/logic coming from the Web server in a very non-invasive way, since it doesn't involve a full postback.  Site like GMail and Google Complete use these techniques to provide enhanced usability.  Implementing script callback is really amazingly easy with ASP.NET 2.0 - all the plumbing is taken care of for you, you just need to write the server-side callback method, inject the appropriate JavaScript to initiate the callback, and write the JavaScript function to handle the returned data.  More info available at Dino Esposito's Custom Script Callbacks in ASP.NET article.

In fact, I whipped up a little demo for a talk I'll be doing later on in the year.  You can download the code sample here.

posted on Thursday, January 13, 2005 3:33 PM

Feedback

# re: Enhanced Client-Side Script Features in ASP.NET 2.0 1/13/2005 4:39 PM Rocky Moore

I am wondering though, if this is an IE only solution. I would hope that there is some form of cross browser compatibility, but would not be surprised if it was IE only.

BTW, I have enjoyed your RssFeed control :)

# re: Enhanced Client-Side Script Features in ASP.NET 2.0 1/13/2005 5:29 PM Scott Mitchell

Rocky, I assume you are referring to the callback script? It uses the XmlHTTP component that is found in a lot of modern browers (FireFox, latter version of Mozilla & Netscape, etc.)

# re: Enhanced Client-Side Script Features in ASP.NET 2.0 1/13/2005 5:42 PM Scott Mitchell

I meant to post this in my blog entry, but couldn't find the URL. Anywho, here's another great article on creating callback scripts:
http://fredrik.nsquared2.com/viewpost.aspx?PostID=259">http://fredrik.nsquared2.com/viewpost.aspx?PostID=259

It's by Fredrik Normen, who has quite a collection of blog entries on .NET 2.0 on his blog - http://fredrik.nsquared2.com/ - subscribed!

# re: Enhanced Client-Side Script Features in ASP.NET 2.0 1/17/2005 3:56 AM Bob

Most of this sounds like changing the current registerstartupscript() to make me think more about the language of asp.net versus the ease now of having just one way to have to do it and just throwing my clientside script in there. This is a typical microsoft pain in the behind way of having to be language centric instead of just coding. The last part sounds good, the script callback, unless it ends up being as buggy as SmartNavigation is now.

# Client script in asp.net 1/18/2005 11:47 AM Li Chen's Weblog

# AJAX and ASP.NET Resources 3/14/2005 4:00 PM Robin Curry

# AJAX and ASP.NET Resources 4/9/2005 10:46 AM Robin Curry

# re: Enhanced Client-Side Script Features in ASP.NET 2.0 5/3/2005 12:16 AM Bharat

Is there any way to disable the script to run when the page is refreshed.

# Bill Evjen's Learn ASP.NET Page 8/17/2005 3:03 PM Bill Evjen's Blog

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles