Organizational Research By

Surprising Reserch Topic

how to detect track postback in javascript

how to detect track postback in javascript  using -',javascript,postback'

asked Sep 10, 2015 by EliBelmore
0 votes

Related Hot Questions

7 Answers

0 votes


Client-side Script:

function isPostBack() { //function to check if page is a postback-ed one
  return document.getElementById('_ispostback').value == 'True';

PS: I have not tested it but I've done somthing similar before and it works.

answered Sep 10, 2015 by MelvaLattimo
0 votes

In some cases, you may want to check for postback without any server-side code. For example, in SharePoint, you cannot have code blocks in SharePoint Designer pages, so you can't use any solution that requires <%=something %>. Here is an alternative that involves no server-side code:

One caveat (or feature, depending on how you look at it), this will detect not just postbacks, but any instance where the page links to itself.

answered Sep 10, 2015 by Zenaida4575
0 votes

If you want to check whether the current page will be a postback if the user clicks on a submit button, you can check for the presence of ViewState:

You can use something like document.getElementById("__VIEWSTATE") or the jQuery equivalent.

However, if you want to see whether the current page was generated in response to a postback, then you need to insert that data into the page on the server side first.

For example:

function isPostBack() {
  return <%= Page.IsPostBack %>;
answered Sep 10, 2015 by MazieBuckqrh
0 votes

As JavaScript shouldn't be written with server-side code, and injecting new elements into the page seems like overkill, it seems to me that the simplest solution is to add [datat-*] attributes to the element:

In Page_Load:
Page.Header.Attributes["data-is-postback"] IsPostBack ? "true" : "false";

This can then be accessed as:

Vanilla JS:
document.head.getAttribute('data-is-postback') === 'true';

Of course, if you treat the [data-is-postback] attribute as a boolean attribute, you could alternatively use:

In Page_Load:
if (IsPostBack)
    Page.Header.Attributes.Add("data-is-postback", "");
Vanilla JS:
answered Sep 10, 2015 by Jes89L
0 votes

See following:

answered Sep 10, 2015 by EusebioRugglesu
0 votes

You can only keep track of the postback if you are using AJAX requests or have a hidden field of some sort that the javascript reads on page load. Otherwise the page is regenerated and all POST data is lost; as you would expect and hope.

answered Sep 10, 2015 by Ben4092
0 votes

I have a solution that worked for me.

// Postback catch
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(function (s, e) {
    alert("post back");
answered Sep 10, 2015 by BarbraEsters