How to Address Other Controls' IDs?

A

Axel Dahmen

Hi,

I've created a User Defined Control (.cs class) using JavaScript to address
another control. This only works when my UDC resides on the .aspx page
itself. If I add it to a Web User Control (.ascx), the target control's ID
attribute gets changed by ASP.NET and I can't address it anymore from my
generated JavaScript code.

Can someone please enlighten me on the following: How can I get the prefix
of the container my UDC is in? Is there a function available telling me if
the target ID belongs to a server control (Runat="server")?


Here's some more information:

My UDC basically provides a property allowing the web page author to enter
the ID of the targeted control, like:

<myNS:myCtrl Runat="server" ID="myControl" TargetID="MoveIt"/>
...
<asp:panel Runat="server" ID="MoveIt"></asp:panel>


In the source of my UDC I generate JavaScript to address the other control:

Page.RegisterClientScriptBlock("x"
, "<script>document.getElementById(" + TargetID + ")...</script>");

If TargetID points to a server control, I'd like to be able to add the
context prefix of my UDC's container to the generated JavaScript code.

TIA,
Axel Dahmen
 
B

Brock Allen

Your javascript in the client can't have a hardcoded ID for the control.
Instead you're going to have to dynamically alter & emit your javascript
to take into consideration whatever that ID will be. The way you determine
the ID in the browser is to ask the control on the server via the control's
ClientID property.
 
A

Axel Dahmen

Actually this is what I did. See the C# excerpt below. It's from my UDC's
Page_Init() function.



--------
 
B

Brock Allen

I simply think you need to do:

Page.RegisterClientScriptBlock("x"
, "<script>document.getElementById('" + myControl.ClientID +
'")...</script>");

Note the additional single quotes added to getElementById
 
A

Axel Dahmen

Thanks Allen, still I've found the solution meanwhile:
ctrl.NamingContainer+"_"ctrl.ClientID does the job to qualify a control's id
for javaScript.

Axel

------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top