在 jQuery 或 javascript 中是否有“if (!Page.IsPostBack)”之类的东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7128284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there such a thing as 'if (!Page.IsPostBack)' in jQuery or javascript?
提问by guanome
Is there a way to detect if when the page loads it is a postback or just the page loading?
有没有办法检测页面加载时是回发还是页面加载?
回答by James Hill
JavaScript has no concept of post back. The simplest way to detect this client-side would be to have [Insert Your Server Side Language Here]write/set a JavasScript variable on post back.
JavaScript 没有回发的概念。检测此客户端的最简单方法是让[在此处插入您的服务器端语言]在回发时编写/设置 JavasScript 变量。
In C#, it would look a bit like this:
在 C# 中,它看起来有点像这样:
ClientScript.RegisterClientScriptBlock(GetType(),
"isPostBack",
String.Format("var isPostback = {0};", IsPostBack.ToString().ToLower()),
true);
JavaScript:
JavaScript:
if(isPostback) {
// Postback specific logic here
}
回答by Krizzz
I use an asp:hiddenfield which gets its value on page_load.
我使用了一个 asp:hiddenfield,它在 page_load 上获取它的值。
On the client you can get the value as a string using jQuery, compare it to 'true' resulting in a boolean.
在客户端上,您可以使用 jQuery 将值作为字符串获取,将其与“true”进行比较,从而得到一个布尔值。
HTML:
HTML:
<asp:HiddenField runat="server" ID="hdnIsPostback" />
VB.NET (in page_load):
VB.NET(在 page_load 中):
Me.hdnIsPostback.Value = Me.IsPostBack
Javascript:
Javascript:
var isPostback = $("#<%=hdnIsPostback.ClientID%>").val().toLowerCase() === "true";