user1143801
user1143801

Reputation: 27

How to access all asp controls using jQuery?

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?

Upvotes: 1

Views: 668

Answers (2)

gideon
gideon

Reputation: 19465

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 () {

Upvotes: 2

Shadow Wizzard
Shadow Wizzard

Reputation: 66397

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

$("input[type=submit]").click(function () {
    //...
});

Upvotes: 1

Related Questions