javascript - How to tell if a page unload in ASP is a PostBack - Stack Overflow

This seems like a mon question but search is not returning anything.I have the following code that exec

This seems like a mon question but search is not returning anything.

I have the following code that executes before the page unloads.The problem is if the unload is a postback i do not want to fire my warning to the user but i can't figure out how to differentiate between a postback and a user navigating to another page for example.

// This is executed before the page actually unloads
        $(window).bind("beforeunload", function () {

            if (prompt) {

                //prompt
                return true;
            }
            else {

                //reset our prompt variable
                prompt = true;
            }
        })

Running script in the code behind i.e. if Page.IsPostBack then set prompt is not an option.

Any ideas?

EDIT:

Here is the solution I ended up with:

 function DoNotPrompt() {
              prompt = false;
        }

I then added this to all the controls where the user could do something that result in a post back.

OnClientClick="DoNotPrompt()

Then checked this flag and only returned a string in "beforeunload" if the user was really moving away from the page i.e. not a postback.

I also had to use this code: var magicInput = document.getElementById('__EVENTTARGET');

    if (magicInput && magicInput.value) {
        // the page is being posted back by an ASP control 
        prompt = false;
    }

The reason being i had a custom user control that was a list box and I could not add the above method. So used this to catch that event and set the flag to false.

Not the most elegent solution.

Thanks, Michael

This seems like a mon question but search is not returning anything.

I have the following code that executes before the page unloads.The problem is if the unload is a postback i do not want to fire my warning to the user but i can't figure out how to differentiate between a postback and a user navigating to another page for example.

// This is executed before the page actually unloads
        $(window).bind("beforeunload", function () {

            if (prompt) {

                //prompt
                return true;
            }
            else {

                //reset our prompt variable
                prompt = true;
            }
        })

Running script in the code behind i.e. if Page.IsPostBack then set prompt is not an option.

Any ideas?

EDIT:

Here is the solution I ended up with:

 function DoNotPrompt() {
              prompt = false;
        }

I then added this to all the controls where the user could do something that result in a post back.

OnClientClick="DoNotPrompt()

Then checked this flag and only returned a string in "beforeunload" if the user was really moving away from the page i.e. not a postback.

I also had to use this code: var magicInput = document.getElementById('__EVENTTARGET');

    if (magicInput && magicInput.value) {
        // the page is being posted back by an ASP control 
        prompt = false;
    }

The reason being i had a custom user control that was a list box and I could not add the above method. So used this to catch that event and set the flag to false.

Not the most elegent solution.

Thanks, Michael

Share Improve this question edited May 2, 2012 at 16:08 Michael Hollywood asked May 2, 2012 at 13:19 Michael HollywoodMichael Hollywood 2695 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You can capture the submit and reset the onbeforeunload as:

jQuery(function($) {
    var form = $('form'), oldSubmit = form[0].onsubmit;
    form[0].onsubmit = null;

    $('form').submit(function() {       
        // reset the onbeforeunload
        window.onbeforeunload = null;

        // run what actually was on
        if(oldSubmit)           
            oldSubmit.call(this);           
    });
});

This is a tested code from my pages :)

This may not cover all of the postback situations, but you can tell if the page was posted back by an ASP control by interrogating the __EVENTTARGET hidden input.

This input is set by ASP when the page is posted back by an ASP control.

var magicInput = document.getElementById('__EVENTTARGET');

if (magicInput && magicInput.value) {
   // the page is being posted back by an ASP control
}

JavaScript runs on the client; as far as the client is concerned, a page does not maintain state from one view to the next. Postbacks are entirely an ASP.NET concept.

You can get around this by running some code on the server-side which defines a JavaScript variable based on whether or not Page.IsPostBack is true.

Example:

<%@ Page Language="C#" AutoEventWireup="true"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3/1999/xhtml">
<head runat="server">
    <title>Page.IsPostBack -> client</title>

    <script type="text/javascript">
        var isPostback = <%= Page.IsPostBack %>;
        console.log("IsPostBack: " + isPostback);
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button runat="server" ID="btnTest" Text="Click me..." />
    </div>
    </form>
</body>
</html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744186972a4562250.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信