1

I'm trying to set window.onbeforeunload to null for every asp Button as they are prompting my "Are you sure you want to close browser" message even they the button is just redirecting them.

$('#asp:button').click(function () {
                alert("asp test working");
                window.onbeforeunload = null;
        });

So this is the above code i'm trying to access all asp:button to set window.onbeforeunload to null.

function confirmExit() { 
        return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?"; } 

And this is my prompt code.

For some reason, I can't get the first section code to work, the alert dont even appear!

Any pointers?

1
  • The ID of the buttons isn't "asp:button". Take a look in the html in the browser and see common class to use. Commented Feb 5, 2012 at 9:49

2 Answers 2

1

Since <asp:Button> is translated to ordinary submit button, you need such selector:

$("input[type=submit]").click(function () {
    //...
});
Sign up to request clarification or add additional context in comments.

Comments

1

Basically #asp:button isn't the ID of your submit buttons. Asp.net gives them a unique ID which can be accessed in your aspx views like this <%=myBtn.ClientID%>

In your view (.aspx page) you could loop through all your asp.net buttons and access them with <%=myBtn.ClientID%>

OR you could just set the selector to all inputs, like @ShadowWizard mentions:

$("input[type=submit]")

OR, you could set a the CssClass property (See here) for all your target buttons on page load or something and then target them in jquery from that class.

Lets say you do set all buttons.CssClass = "mybuttons";

Then you JS would be:

$('.mybuttons').click(function () {

3 Comments

OP mentioned every asp Button - sounds like he got more than one, and no need to specify each.
Yea sorry overlooked that somehow!
Thanks guys! very insightful knowledge to remember!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.