The expando attribute

by Mads Kristensen 19. October 2007 02:49

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

Tags:

ASP.NET | Client-side

Comments

10/19/2007 3:38:42 AM #

Josh Stodola

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!

Josh Stodola United States |

10/19/2007 8:42:23 AM #

Ryan Anderson

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

Ryan Anderson United States |

10/19/2007 12:24:54 PM #

Dityo Nurasto

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

Dityo Nurasto Indonesia |

10/20/2007 5:28:22 AM #

Chris

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.

Chris United States |

10/20/2007 10:04:51 AM #

trackback

Trackback from DotNetKicks.com

The expando attribute

DotNetKicks.com |

10/22/2007 3:29:02 PM #

Einar

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".

Einar Norway |

11/20/2007 10:11:12 AM #

brent warner

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.

brent warner United Kingdom |

9/19/2008 4:26:41 PM #

pingback

Pingback from blog.news-lan.ru

Интересное в сети» Архив блога » Важно и интересно:Полезная функция - RegisterExpandoAttribute

blog.news-lan.ru |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008