Groups | Blog | Home
all groups > asp.net webcontrols > november 2005 >

asp.net webcontrols : How to Address Other Controls' IDs?


Axel Dahmen
11/21/2005 12:00:00 AM
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

Brock Allen
11/21/2005 9:00:38 AM
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen

[quoted text, click to view]

Brock Allen
11/21/2005 10:18:36 AM
I simply think you need to do:

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

Note the additional single quotes added to getElementById

-Brock
DevelopMentor
http://staff.develop.com/ballen

[quoted text, click to view]

Axel Dahmen
11/21/2005 6:39:49 PM
Actually this is what I did. See the C# excerpt below. It's from my UDC's
Page_Init() function.



--------
[quoted text, click to view]

Axel Dahmen
11/22/2005 12:00:00 AM
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

------------
[quoted text, click to view]

AddThis Social Bookmark Button