Home All Groups Group Topic Archive Search About

How to Address Other Controls' IDs?

Author
21 Nov 2005 4:30 PM
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

Author
21 Nov 2005 5:00 PM
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.

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

Show quoteHide quote
> 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
Author
21 Nov 2005 5:39 PM
Axel Dahmen
Actually this is what I did. See the C# excerpt below. It's from my UDC's
Page_Init() function.



--------
Show quoteHide quote
"Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
news:b8743b114ee7e8c7bcaa52522485@msnews.microsoft.com...
> 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
>
> > 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
>
>
Author
21 Nov 2005 6:18 PM
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

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

Show quoteHide quote
> Actually this is what I did. See the C# excerpt below. It's from my
> UDC's Page_Init() function.
>
> --------
> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
> news:b8743b114ee7e8c7bcaa52522485@msnews.microsoft.com...
>> 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
>>> 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
Author
22 Nov 2005 11:05 AM
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

------------
Show quoteHide quote
"Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
news:b8743b114f3208c7bcb5368611a7@msnews.microsoft.com...
> 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
>
> > Actually this is what I did. See the C# excerpt below. It's from my
> > UDC's Page_Init() function.
> >
> > --------
> > "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
> > news:b8743b114ee7e8c7bcaa52522485@msnews.microsoft.com...
> >> 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
> >>> 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
>
>