views:

712

answers:

4

Hi, Is it possible to determine if an ASP.NET page is a postback from javascript? So basically a javascript equivalent of C# Page.IsPostBack.

Thanks.

+9  A: 

I would just put a render tag in the javascript

var isPostBack = <%= Page.IsPostback ? "true" : "false" %>;

Putting the "true" and "false" as strings should be done to eliminate any possible issues converting the type to a string as in C# true.ToString() is usually "True" which is an error in javascript.

Bob
This is something we do as well, very handy +1
Kezzer
Of course, at this point, it is technically "wasPostBack". :P
patridge
+2  A: 

Sure. Just use your server code to write out a javascript flag, if you really need this.

But I suspect you're barking up the wrong tree here.

Joel Coehoorn
A: 

I'm not exactly sure of what you are trying to do but you might want to look into the PageRequestManagerClass:

http://www.asp.net/AJAX/Documentation/Live/ClientReference/Sys.WebForms/PageRequestManagerClass/default.aspx

It exposes events for all different parts of the request.

Robb C
A: 

Just a minor detail here, C# (and JavaScript) are case-sensitive languages so the accepted answer above will not work.

It should be:

var isPostBack = <%= Page.IsPostBack ? "true" : "false" %>; 

vs.

var isPostBack = <%= Page.IsPostback ? "true" : "false" %>; 

Pre-emptive snarky comment: Please use the comment feature for corrections to answers like this.

(I would have commented had I enough rep to do so but haven't)

noonand
Both of your lines of code are the same and are the same as the accepted answers.
Bob
Bob, Just double checked. They are most definitely *not* the same :-) Hint: look at the IsPostBack part
noonand