Replacing <%= Control.ClientID %> in external javascript
Replacing <%= Control.ClientID %> in external javascript
am 22.01.2008 16:31:59 von 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
Re: Replacing <%= Control.ClientID %> in external javascript
am 22.01.2008 16:54:39 von mark
"MarkShoe" wrote in message
news:E2C4DBF0-704E-41FF-A7A3-2843FCBAF01F@microsoft.com...
> 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 ;
}
// in aspx page
var varResult =
myFunction(document.getElementById('<%=myControl.ClientID%>'));
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Re: Replacing <%= Control.ClientID %> in external javascript
am 22.01.2008 17:00:18 von 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?
"Mark Rae [MVP]" wrote in message
news:ep9te8QXIHA.4532@TK2MSFTNGP02.phx.gbl...
> "MarkShoe" wrote in message
> news:E2C4DBF0-704E-41FF-A7A3-2843FCBAF01F@microsoft.com...
>
>> 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 ;
> }
>
> // in aspx page
> var varResult =
> myFunction(document.getElementById('<%=myControl.ClientID%>'));
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net
Re: Replacing <%= Control.ClientID %> in external javascript
am 22.01.2008 17:04:46 von mark
"MarkShoe" wrote in message
news:%23R6Cl$QXIHA.4696@TK2MSFTNGP05.phx.gbl...
>>> 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 ;
>> }
>>
>> // in aspx page
>> var varResult =
>> myFunction(document.getElementById('<%=myControl.ClientID%>'));
>
> 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...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Re: Replacing <%= Control.ClientID %> in external javascript
am 22.01.2008 17:11:09 von Registered User
On Tue, 22 Jan 2008 10:31:59 -0500, "MarkShoe"
wrote:
>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.
Re: Replacing <%= Control.ClientID %> in external javascript
am 23.01.2008 03:58:32 von stcheng
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"
>References:
>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?
>
>
>
>"Mark Rae [MVP]" wrote in message
>news:ep9te8QXIHA.4532@TK2MSFTNGP02.phx.gbl...
>> "MarkShoe" wrote in message
>> news:E2C4DBF0-704E-41FF-A7A3-2843FCBAF01F@microsoft.com...
>>
>>> 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 ;
>> }
>>
>> // in aspx page
>> var varResult =
>> myFunction(document.getElementById('<%=myControl.ClientID%>'));
>>
>>
>> --
>> Mark Rae
>> ASP.NET MVP
>> http://www.markrae.net
>
>
Re: Replacing <%= Control.ClientID %> in external javascript
am 23.01.2008 16:22:18 von 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("External JavaScript",
"external.js");
This works fine for my purposes.
"Steven Cheng[MSFT]" wrote in message
news:BKi9kvWXIHA.360@TK2MSFTNGHUB02.phx.gbl...
> 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"
>>References:
>
>>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?
>>
>>
>>
>>"Mark Rae [MVP]" wrote in message
>>news:ep9te8QXIHA.4532@TK2MSFTNGP02.phx.gbl...
>>> "MarkShoe" wrote in message
>>> news:E2C4DBF0-704E-41FF-A7A3-2843FCBAF01F@microsoft.com...
>>>
>>>> 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 ;
>>> }
>>>
>>> // in aspx page
>>> var varResult =
>>> myFunction(document.getElementById('<%=myControl.ClientID%>'));
>>>
>>>
>>> --
>>> Mark Rae
>>> ASP.NET MVP
>>> http://www.markrae.net
>>
>>
>
Re: Replacing <%= Control.ClientID %> in external javascript
am 24.01.2008 03:49:01 von stcheng
Thanks for your reply and share us the solution.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: "MarkShoe"
>References:
<#R6Cl$QXIHA.4696@TK2MSFTNGP05.phx.gbl>
>Subject: Re: Replacing <%= Control.ClientID %> in external javascript
>Date: Wed, 23 Jan 2008 10:22:18 -0500
>
>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("Externa lJavaScript",
>"external.js");
>
>
>This works fine for my purposes.
>
>
>"Steven Cheng[MSFT]" wrote in message
>news:BKi9kvWXIHA.360@TK2MSFTNGHUB02.phx.gbl...
>> 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"
>>>References:
>>
>>>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?
>>>
>>>
>>>
>>>"Mark Rae [MVP]" wrote in message
>>>news:ep9te8QXIHA.4532@TK2MSFTNGP02.phx.gbl...
>>>> "MarkShoe" wrote in message
>>>> news:E2C4DBF0-704E-41FF-A7A3-2843FCBAF01F@microsoft.com...
>>>>
>>>>> 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 ;
>>>> }
>>>>
>>>> // in aspx page
>>>> var varResult =
>>>> myFunction(document.getElementById('<%=myControl.ClientID%>'));
>>>>
>>>>
>>>> --
>>>> Mark Rae
>>>> ASP.NET MVP
>>>> http://www.markrae.net
>>>
>>>
>>
>