有没有办法使用&QUOT;&LT;%= someObject.ClientID%GT;&QUOT;在外部JavaScript文件? [英] Is there a way to use "<%= someObject.ClientID %>" in an external javascript file?
问题描述
有没有办法使用办法&LT;%= someObject.ClientID%>?在外部JavaScript文件
Is there a way to use "<%= someObject.ClientID %>" in an external javascript file?
如果我用code
<%= someObject.ClientID %>
在里面我作为(C / P)×页中的脚本标记,它工作正常。在呈现的页面,ClientID的解决。
Howvever,如果我把一个外部JS文件,只需要添加:
in a script tag inside my as(c/p)x page, it works fine. On the rendered page, the ClientID is resolved. Howvever, if I put in an external JS file and just add:
它没有。有没有办法做到这一点还是我坚持离开那个code。在为(C / P)×文件?
It doesn't. Is there a way to do this or am I stuck with leaving that code in the as(c/p)x file?
侧的问题 - 什么是做&LT的行为;在一个名为您的标记文件%= ...%>
Side question -- what is the act of doing <%=... %> in your markup file called?
推荐答案
这是完全可能的。
在您的presentation层(你的.aspx页面),创建一个脚本引用一个aspx页面包含你的JavaScript code:
In your presentation layer (your .aspx page), create a script reference to an aspx page that contains your javascript code:
<script src="../MyJavaScriptFile.js.aspx" type='text/javascript'></script>
然后,在MyJavaScriptFile.js.aspx,你可以写:
Then, in MyJavaScriptFile.js.aspx, you can write the following:
<%@ Page Language="C#" AutoEventWireup="false" ContentType="text/javascript" %>
<%
CustomPersonObject cpt = new CustomPersonObject();
cpt.firstname = "John";
cpt.lastname = "Smith";
%>
// Start Javascript
var personFirstName = '<% HttpContext.Current.Response.Write(cpt.name);%>';
var personLastName = '<% HttpContext.Current.Response.Write(cpt.lastname);%>'
一种方法我经常使用的是通过我在javascript文件需要评估为初始脚本引用的URL参数的值:
One technique I frequently use is to pass the value I need evaluated in my javascript file as a url parameter in the initial script reference:
<script src="../MyJavaScriptFile.js.aspx?username=<% somevalue %>"
type='text/javascript'></script>
然后,在MyJavaScriptFile.js.aspx,我可以参考值
Then, in MyJavaScriptFile.js.aspx, I can reference the value with
<% Request.QueryString["username"] %>
这不是最佳实践的方式做事情,但它得到的方式,我的穴居人的大脑可以不必诉诸于花哨的变通方法,并额外的code明白所做的工作。
It's not the "best practice" way to do things, but it gets the job done in a way that my caveman brain can understand without having to resort to fancy workarounds and extra code.
这篇关于有没有办法使用&QUOT;&LT;%= someObject.ClientID%GT;&QUOT;在外部JavaScript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!