How do you get the ID of User Control in client script?

  • Thread starter Thread starter Ken Varn
  • Start date Start date
K

Ken Varn

I have created a User Control (ASCX) and I need to be able to do some
JavaScript on the HTML side on some controls that are in the User Control.
How do I get the ID of the Controls within a User Control? Asp.net appends
each control with the ID of the ASCX control. Some controls are in the
format of ParentID:ControlID, and others are in the format of
ParentID_ControlID. Is there some method in JavaScript to determine the
true ID of the control with the pre-pended ASCX ID?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
 
You must determine this on the server side using MyControl.ClientID.
Then pass this ID to your client side code so it can use it.
 
Thanks for your reply, but I actually found a solution that works well for
me.

I basically set a JavaScript variable to <%=ID%>. Then, I have the base ID
of the User Control and can then append that to any controls IDs that I
reference in Script. The only thing that I don't understand is why some
controls have a colon (:) between the UserControl ID and the Child Control
ID, and other IDs use an underscore (_)? Is there any rhyme or reason to
this?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

Emailed = varnk
Domain = Diebold.com
-----------------------------------
Steve C. Orr said:
You must determine this on the server side using MyControl.ClientID.
Then pass this ID to your client side code so it can use it.
 
That's why you need to get the ClientID, not just the ID.
The reason they get named such ways is all about naming containers.
Here's more info:
http://msdn.microsoft.com/library/d...l/vbtskreferencingcontrolsinwebformspages.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net


Ken Varn said:
Thanks for your reply, but I actually found a solution that works well for
me.

I basically set a JavaScript variable to <%=ID%>. Then, I have the base
ID
of the User Control and can then append that to any controls IDs that I
reference in Script. The only thing that I don't understand is why some
controls have a colon (:) between the UserControl ID and the Child Control
ID, and other IDs use an underscore (_)? Is there any rhyme or reason to
this?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

Emailed = varnk
Domain = Diebold.com
 
Back
Top