Saturday, March 24, 2012

Passing <%= myControl.ClientID %> into Javascript function

I can successfully use <%=myControl.ClientID%> inside of a Javascript
function, but does anyone know if it is possible to pass this value to
my Javascript function? The reason I want to do this is so I can
abstract my Javascript function out of my ASP.NET user control.
Ideally, I would like to have a function like this.
function testMe(control){
document.getElementByID(control).style = ...
// do more useful things here
// I am trying not to have to use this...
// document.getElementByID('<%=btnTest.ClientID%>').style = ...
}
I would like to be able to call it like this...
<asp:LinkButton id="btnTest" runat="server" OnClientClick="return
testMe(<%=this.ClientID%> )" text="test" />
But this does not work. If I have to keep the actual control name in
the javascript file, then my decoupling is lost. Is this approach
possible?
Thanks.Alex,
why not just pass the reference to the control itself, rather than its ID:
<asp:LinkButton id="btnTest" runat="server" OnClientClick="return
testMe(this);" text="test" />
function testMe(control){
control.style = ...
}
"Alex" wrote:

> I can successfully use <%=myControl.ClientID%> inside of a Javascript
> function, but does anyone know if it is possible to pass this value to
> my Javascript function? The reason I want to do this is so I can
> abstract my Javascript function out of my ASP.NET user control.
> Ideally, I would like to have a function like this.
> function testMe(control){
> document.getElementByID(control).style = ...
> // do more useful things here
> // I am trying not to have to use this...
> // document.getElementByID('<%=btnTest.ClientID%>').style = ...
> }
> I would like to be able to call it like this...
> <asp:LinkButton id="btnTest" runat="server" OnClientClick="return
> testMe(<%=this.ClientID%> )" text="test" />
> But this does not work. If I have to keep the actual control name in
> the javascript file, then my decoupling is lost. Is this approach
> possible?
> Thanks.
>
On Jul 26, 11:56 am, Sergey Poberezovskiy
<SergeyPoberezovs...@.discussions.microsoft.com> wrote:
> Alex,
> why not just pass the reference to the control itself, rather than its ID:
> <asp:LinkButton id="btnTest" runat="server" OnClientClick="return
> testMe(this);" text="test" />
> function testMe(control){
> control.style = ...
> }
> "Alex" wrote:
>
>
>
>
>
>
hi...
Sergey Poberezovskiy's suggested method is right...
in this way you dont have to think about client id...
Thanks
Md. Masudur Rahman (Munna)
Kaz Software Ltd.
www.kaz.com.bd
http://munnacs.110mb.com
Passing in "this" did the trick. I tried that before, but for some
reason I thought I was getting the control's ID at design time, not
the actual ASP.NET client ID at run time. This works. I appreciate the
help.

0 comments:

Post a Comment