检查外部 Javascript 文件中的 IsPostBack
我需要从外部 Javascript 文件检查 IsPostBack
(ASP.NET 页面)。这是我在谷歌搜索后发现的:
var isPostBack = <%= Page.IsPostBack ? "true" : "false" %>;
但是 <%=
%>
似乎在外部 JS 文件中无法被识别。如果是这样,替代解决方案是什么?
From an extrnal Javascript file, I need to check for IsPostBack
(ASP.NET page). Here is what I found after googling:
var isPostBack = <%= Page.IsPostBack ? "true" : "false" %>;
But <%=
%>
doesn't seem to be recognized in the external JS file. If so, what is the alternate solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这在 Javascript 文件中不起作用,因为服务器将照此提供服务并且无法识别 asp 标签。不过,您可以从 aspx 文件执行此操作,并且它会起作用。您还可以使用通用处理程序。
祝你好运!
This will not work in a Javascript file cause the Server will serve it as such and wont recognize the asp tags. You could do this, however, from a aspx file and it will work. You could also use a Generic Handler.
Good luck!
您只能通过页面上使用的 aspx 页面或用户控件或母版页来呈现此
变量
。You can only render this
variable
through the aspx page or user control or master page used on the page.您将无法从外部文件执行此操作。即使可以,外部文件也会缓存在客户端的浏览器上,并且不会每次都被提取。您也许可以在页面中放置一个函数并从外部脚本调用它。
外部脚本
ASPX 页面(标头中的脚本)
只需确保您的外部脚本在页面中上述行之后加载即可。
You won't be able to do that from an external file. Even if you could, external files get cached on the client's browser, and they don't get pulled every time. You might be able to place a function in the page and call it from the external script.
External Script
ASPX Page (Script in header)
Just make sure that your external script gets loaded after the above line in the page.
对于要处理的 ASP 代码,您需要有一个映射到 IIS 中的 ASP dll 的文件扩展名。
这里最简单的情况是使用
.aspx
扩展名重命名.js
文件,然后更改的 src 属性元素。
或者,在
aspx
页面中全局创建isPostBack
变量,然后调用包含其用法的js
文件,例如:For ASP code to be processed you need to have a file extension which is mapped to the ASP dll in IIS.
The simplest case here would be to rename your
.js
file with a.aspx
extension, then change the src attribute of your<script>
element.Alternatively, create your
isPostBack
variable globally in youraspx
page, and then call yourjs
file which contains its usage, eg:您不能在外部 JavaScript 文件中使用
<%= %>
表示法;这是行不通的。You cannot use
<%= %>
notation in an external JavaScript file; it will not work.