Organizational Research By

Surprising Reserch Topic

how to detect track postback in javascript


how to detect track postback in javascript  using -'asp.net,javascript,postback'
asked Sep 14, 2015 by FreLZZH
0 votes
5 views



Related Hot Questions



Government Jobs Opening

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 14, 2015 by VenusChirnsi
0 votes

See following:


answered Sep 14, 2015 by EliWZW
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:

jQuery:
$('head').data('isPostback');
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", "");
}
else
{
    Page.Header.Attributes.Remove("data-is-postback");
}
jQuery:
$('head').is('[data-is-postback]');
Vanilla JS:
document.head.hasAttribute('data-is-postback')
answered Sep 14, 2015 by JocelynMenzi
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 14, 2015 by AilBethea
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 14, 2015 by FloQwi

...