The expando attribute

Oct 18, 2007

By coincidence I noticed a method I’ve never seen before on the ClientScript property of the page class in ASP.NET 2.0. It’s called something as cryptic as RegisterExpandoAttribute and it’s very useful.

It can set JavaScript properties on any element you would normally reference with document.getElementById(‘elementID’). That means you can control the state of your HTML elements from the code-behind in a very easy manor. It also means you can control HTML elements that don’t have a runat=”server” attribute.

Here are two examples – one where a property is set on a server-control and one on a HTML element otherwise invisible to the code-behind.

Page.ClientScript.RegisterExpandoAttribute(txtPassword.ClientID, "value", "britney");

Page.ClientScript.RegisterExpandoAttribute("maintable", "background", "red"); 

And this is the JavaScript it produces:

<script type="text/javascript">
<!--
var txtPassword = document.all ? document.all["txtPassword"] : document.getElementById("txtPassword");
txtPassword.value = "britney";
// -->
</script>

I think where this really rocks the most is the ability to control regular HTML elements that hasn’t got the runat=”server” attribute. Those elements have always been invisible to the code-behind and now you have direct server-side access to their properties. The method doesn't give you anything you couldn't do before, but it makes it so much easier and cleaner.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Comments (6) -

Josh Stodola
Josh Stodola United States
10/18/2007 6:38:42 PM #

RegisterExpandoAttribute?!  Wow.  They really gave that function a meaningful name, didnt they?!

I wonder why it even bothers to check document.all, considering that getElementById has been a standard for nearly a decade now.

http://simonwillison.net/2003/Aug/11/documentAll/

Thanks for sharing!

Ryan Anderson
Ryan Anderson United States
10/18/2007 11:42:23 PM #

You're always finding the kind nug's Mads.
Tanx!
RA

Dityo Nurasto
Dityo Nurasto Indonesia
10/19/2007 3:24:54 AM #

Woohoo! thanks for sharing, Mads. MasterPage feature become easier to maintain Smile.

Chris
Chris United States
10/19/2007 8:28:22 PM #

Regarding document.all vs. document.getElementById - perhaps document.all has better performance in IE, even with the if statement? Don't know really - just guessing.

Einar
Einar Norway
10/22/2007 6:29:02 AM #

I believe the primary purpose of this method is to enable you to expand the properties of an object on the client, without breaking xhtml.
A typical example would be:
Page.ClientScript.RegisterExpandoAttribute("foobar", "foo", "bar");

Can of course also be used for setting things like "value".

brent warner
brent warner United Kingdom
11/20/2007 1:11:12 AM #

As per Josh Stodola and Chris asking about the document.all[id] vs document.getElementById(id), it is *VERY* important to know that they are totally different, and the IE version of document.getElementById() is horribly broken!

webbugtrack.blogspot.com/.../...tbyid-returns.html

Using the code as outlined, will cause you huge issues if your site uses id's and name's interchangably.  They are quite different, and should be treated as such.

Best of luck, and be sure to test in non-IE browsers to ensure that this bug in IE isn't making your site fail in other browsers.

Pingbacks and trackbacks (2)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.