Is there a way to detect if when the page loads it is a postback or just the page loading?
Asked
Active
Viewed 1.8k times
9
-
As in as .Net postback ? – Russ Clarke Aug 19 '11 at 23:04
-
This has been asked a lot, the accepted answer here is a good example of what James suggests: http://stackoverflow.com/questions/59719/how-can-i-check-for-ispostback-in-javascript – Russ Clarke Aug 19 '11 at 23:09
-
@Russ I figured it had been asked a lot, but I couldn't find any references to it. – FarFigNewton Aug 19 '11 at 23:15
2 Answers
16
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.
In C#, it would look a bit like this:
ClientScript.RegisterClientScriptBlock(GetType(),
"isPostBack",
String.Format("var isPostback = {0};", IsPostBack.ToString().ToLower()),
true);
JavaScript:
if(isPostback) {
// Postback specific logic here
}

James Hill
- 60,353
- 20
- 145
- 161
1
I use an asp:hiddenfield which gets its value on page_load.
On the client you can get the value as a string using jQuery, compare it to 'true' resulting in a boolean.
HTML:
<asp:HiddenField runat="server" ID="hdnIsPostback" />
VB.NET (in page_load):
Me.hdnIsPostback.Value = Me.IsPostBack
Javascript:
var isPostback = $("#<%=hdnIsPostback.ClientID%>").val().toLowerCase() === "true";

Krizzz
- 21
- 2