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

A

Alex

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.
 
G

Guest

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 = ...
}
 
M

Masudur

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 = ...

}

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
 
A

Alex

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.
 
Joined
Jul 15, 2008
Messages
1
Reaction score
0
The solution i found

Hi, i had the same problem. This is the solution

protected void Page_Load(object sender, EventArgs e)
{
btnTest.OnClientClick = String.Format("return testMe({0})", btnTest.ClientID);

...
}
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top