Thomas’ Developer Blog

October 12, 2008

Quick tip on client side IDs in custom controls for javascript use.

Filed under: Asp.Net Framework 2.0, Custom Controls, javascript — Tags: , , , , , , , — sanzon @ 7:23 pm

Well one thing I found annoying when developing custom controls that use javascript is what do you do when you need to give an ID to a html dom object within a custom control in order to use it with javascript!

I thought at first, ok I could do some complex child/parent relationship coding, which I try not to do a ton of, but it works really well at times. In this case it would be too much of a pain. So I came up with another solution to this little problem and it’s not hard when you think about it.

ClientID’s often look like ct100_SomeID_SomeChildID_ETC

Well if you think about valid ID names, ID’s cannot start with a number! So the answer is pretty simple when you think about it.

Simply take the base ID of ct100_SomeID and attach a string such as, “_100jso_myID”

It produces something like:
ct100_SomeID_100jso_ClientSideID

The great part about using a numeric value first is that it’s impossible to use a child control named 100jso, so it passes just fine. In case you’re wondering jso stands for javascript object in this case. I guess hdo, or html dom object may make more sense. Long as it starts with a number it’s safe!

So when you do work with javascript simple call:

document.getElementById(‘” & myBase.clientID & “_101jso_ClientID”‘)

Hope this helps

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.