Replacing <%= Control.ClientID %> in external javascript

M

MarkShoe

Hi,

I have an aspx with a control on it, lets call it myControl, and I have an
external javascript file.

In the external javascript file, I have

var myControlId = '<%=myControl.ClientID%>';

which would work fine if the javascript was in the .aspx file, but when
adding the external javascript file via
Page.ClientScript.RegisterClientScriptInclude, the myControl.ClientID does
not get replaced with the client id.

I'm sure I've seen a way to do this before, can someone point me in the
right direction?

Thanks
Mark
 
M

Mark Rae [MVP]

I'm sure I've seen a way to do this before, can someone point me in the
right direction?

// in script file
myFunction(pobjControl)
{
var MyControl = pobjControl;
// rest of processing;
return <whatever you need to return>;
}

// in aspx page
var varResult =
myFunction(document.getElementById('<%=myControl.ClientID%>'));
 
M

MarkShoe

Thanks for your reply,

That is one way to do it, but isn't there a way to make the javascript an
embedded resource and tell it to replace the <%= %> blocks?
 
M

Mark Rae [MVP]

That is one way to do it, but isn't there a way to make the javascript an
embedded resource and tell it to replace the <%= %> blocks?

No idea - sorry...
 
R

Registered User

Hi,

I have an aspx with a control on it, lets call it myControl, and I have an
external javascript file.

In the external javascript file, I have

var myControlId = '<%=myControl.ClientID%>';

which would work fine if the javascript was in the .aspx file, but when
adding the external javascript file via
Page.ClientScript.RegisterClientScriptInclude, the myControl.ClientID does
not get replaced with the client id.

I'm sure I've seen a way to do this before, can someone point me in the
right direction?
As you have found out the ClientIDs need to added to script. I suggest
the myControl type dynamically generate and register its own scripts.
If include files are to be used let the control read the file and
programmatically replace the tagged elements with ClientIDs before
registering the script. The dependencies between the script and the
control might justify the functionality all be in one wrapper.

regards
A.G.
 
S

Steven Cheng[MSFT]

Hi Mark,

As for the following way you mentioned:

===========
Page.ClientScript.RegisterClientScriptInclude, the myControl.ClientID does
not get replaced with the client id.
==========

this is because such expression like "<%= xxxxx%>" is not simply a string
fragment in aspx template, they're code block which will be parsed and
evaluated at runtime(during page's compilation stage). However, if you use
Page.ClientScript.Register .... to add them into page content, they're not
parsed(since the page has already run over the compilation stage) so that
they're output as plain text.

For your scenario, you may either keep the inline <%= %> expression or
programmatically specifiy the id in code. Something like:

Page.ClientScript.RegisterClientScript( script_start + Page.clientID +
script_end);

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "MarkShoe" <[email protected]>
References: <[email protected]>
Subject: Re: Replacing <%= Control.ClientID %> in external javascript
Date: Tue, 22 Jan 2008 11:00:18 -0500
Thanks for your reply,

That is one way to do it, but isn't there a way to make the javascript an
embedded resource and tell it to replace the <%= %> blocks?
 
M

MarkShoe

Ok, thanks for your responses, it seems that it is not as simple as I
expected. Is it ever? ;)

I ended up registering a startup script containing the client ids of the
controls which I needed to access, and then registered the external script.

String scriptVars = string.Format("var controlId =
'{0}';",Control.ClientID);
ClientScript.RegisterStartupScript(this.GetType(), "ScriptVars", scriptVars,
true);

this.Page.ClientScript.RegisterClientScriptInclude("ExternalJavaScript",
"external.js");


This works fine for my purposes.


Steven Cheng said:
Hi Mark,

As for the following way you mentioned:

===========
Page.ClientScript.RegisterClientScriptInclude, the myControl.ClientID does
not get replaced with the client id.
==========

this is because such expression like "<%= xxxxx%>" is not simply a string
fragment in aspx template, they're code block which will be parsed and
evaluated at runtime(during page's compilation stage). However, if you
use
Page.ClientScript.Register .... to add them into page content, they're
not
parsed(since the page has already run over the compilation stage) so that
they're output as plain text.

For your scenario, you may either keep the inline <%= %> expression or
programmatically specifiy the id in code. Something like:

Page.ClientScript.RegisterClientScript( script_start + Page.clientID +
script_end);

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
From: "MarkShoe" <[email protected]>
References: <[email protected]>
Subject: Re: Replacing <%= Control.ClientID %> in external javascript
Date: Tue, 22 Jan 2008 11:00:18 -0500
 

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

Top