2

I have the following code to detect user navigation or close. The post back is triggered on close but not when navigated away. If i place a alert message it is triggered at all times.

Oh and there is a timer on the page which is actually disabled after first tick. but there is always a post back triggers when navigating away with eventtarget timer. I think this may be because i have cleared cache for the previous page.

<script src="Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(window).bind("beforeunload", function () {
        alert("u r navigating away"); // this message gets triggered at all times.
        __doPostBack('callPostBack');
    });
</script>


    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            string eventTarget = this.Request["__EVENTTARGET"];
            string eventArgument = this.Request["__EVENTARGUMENT"];

            if (eventTarget != String.Empty && eventTarget == "callPostBack")
            {
                //do task
            }

        }
    }

Thanks in advance.

Gnani
  • 455
  • 5
  • 18

1 Answers1

0

This is not logical, when the user go way from the page or close it, you can not actually make a post back, think about, the user close the page and you try to reload it with a post back? And if your action works then you have a dead loop, you make post back and keep open the page that user try to close. Anyway if the user close the page, or change it, this is first priority and the post back are not working.

If you try to make ajax call there you also fails because the ajax stops after the page close and this is going to be done (the page close) before the ajax trigger.

You need to re-desing your action.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • How do i do? if i wanna stop some running-functions on the server if the users navigates away. There is a webservice call before it completes the server does not respond to new request. i need to stop this. Thank you for taking time to post. – Gnani Feb 02 '12 at 09:21
  • @Gnani its tricky. Maybe a solution to that is to make a javascript timer with 10 seconds trigger to a handler, and if this is stop calling the handler then the user have done. Need a little design, to add a unique key to know that this is the user that start this session. – Aristos Feb 02 '12 at 09:26
  • @Gnani Other possible way is to show to the user a message on pageunload, just before this message you make an ajax call. At this time the ajax call maybe have the time to inform the server before the user press the dialog ok. – Aristos Feb 02 '12 at 09:28