简体   繁体   中英

Javascript Detect During Postback

I implemented a system on my ASP.NET Web Forms site for passing notifications to the client. The notifications can be validation messages, success messages, warnings, errors etc. These messages are generated by code on the server side and then retrieved from the server via script on the client side and displayed.

On each page of my site I have embedded a JavaScript function that calls a web service (on the server side) via jQuery AJAX and requests the latest notifications. If there are notifications, they are displayed using Noty . On the server side, messages that have been sent to the client are removed from the queue.

This works pretty well. However, let's say I'm on a page called NewEmployee.aspx and they create a new employee by filling out a new form, and this generates some kind of notification such as "New Employee Created With ID 58478" and also takes them to the ViewEmployee.aspx page. If the postback happens slowly enough, the message will start to display while the user is still on NewEmployee.aspx but before the user arrives at ViewEmployee.aspx. The end result is that the notification is only displayed for a split second and the user never sees it.

I need a way in JavaScript on the client side to detect if the page is performing a postback. If I have that, I can prevent it from calling the webservice during a postback and have it wait until it completes. I want it to look something like this.

setInterval(oneSecondFunction, 1000); //check for messages every 1 second

function oneSecondFunction()
{
   var IsPostingBackRightNow=GetIsPostingBackStatus(); //I need your help writing this function
   if(!IsPostingBackRightNow)
   {
      $.ajax({
         type: 'POST',
         url: 'myurl/MessageService.asmx/GetCurrentMessage',
         contentType: 'application/json; charset=utf-8',
         data: JSON.stringify({ sessionid: sessionid, locale: 'en-US' }),
         dataType: 'xml',
         success: function (msg)
         {
            //show message via noty if there's a message
         }
     });
   }
}

The post back is passing from the form , so you capture the onsubmit event on the form, and there you open your flag.

Here is how... add that on code behind

Page.Form.Attributes["onsubmit"] = "return GoForPostBack();";

to have this attribute also rendered on your form...

<form ... onsubmit="return GoForPostBack();">

and the javascript

var IsPostingBackRightNow = false;
function GoForPostBack()
{        
    IsPostingBackRightNow = true;
    return true;
}


setInterval(oneSecondFunction, 1000); //check for messages every 1 second

function oneSecondFunction()
{
   if(!IsPostingBackRightNow)
   {
      $.ajax({
         type: 'POST',
         url: 'myurl/MessageService.asmx/GetCurrentMessage',
         contentType: 'application/json; charset=utf-8',
         data: JSON.stringify({ sessionid: sessionid, locale: 'en-US' }),
         dataType: 'xml',
         success: function (msg)
         {
            //show message via noty if there's a message
         }
     });
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM