1

I'm an ASP.NET beginner. I'm having problems getting my ASP.NET web form to process the external JS file I have listed on the page. Does jQuery have limited functionality with ASP.NET when used externally?

It is processing the first click event on the checkbox, but not the submit button. I've run other tests like alert(), alert without jquery using window.document.onready. I've tried to alter the CSS on elements and manipulate the DOM with jQuery. I've had no success with those techniques either. NOTE: When I use embedded jQuery directly on the web form, everything works fine.

To whom ever responds, Thanks!!!

// THIS IS WHAT IS IN THE EXTERNAL FILE

$(function(){

    $("#CheckBox1").click(function () { // FIRST FUNCTION WORKS FINE

       $('#Panel1').toggle(!this.checked);

    }); // end click  

    $("#SubmitBtn").click(function () { // CONFIRM ON CLICK IS NOT WORKING

        return confirm("Are you sure you want to submit?");

    }); // end click


}); // end ready
2
  • is the button an asp:Button or a <input type=button />?? Commented May 1, 2014 at 15:42
  • 1
    Can u post snippet of ASPX markup showing how SubmitBtn is defined? And if possible snippet of how the same block is rendered in browser as HTML Commented May 1, 2014 at 15:45

3 Answers 3

1

If you're in an external file, you may want to use the CSS class instead of the button ID. Button IDs are dynamic, like the other poster pointed out.

Instead of $('#SubmitBtn'), you could use $('.cssClassname')

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you...for some reason, when I created a new file and copy/pasted the code, everything works. I'm wondering if it was a cache issue. I'll keep the .class suggestion in mind though!
0

Try class for accessing the control..

$(function(){

    $("#CheckBox1").click(function () { // FIRST FUNCTION WORKS FINE

       $('#Panel1').toggle(!this.checked);

    }); // end click  

    $(".class").click(function () { // CONFIRM ON CLICK IS NOT WORKING

        return confirm("Are you sure you want to submit?");

    }); // end click


}); // end ready

Here class is the name of the Css Class

Comments

0

Are you using an ASP.NET server control for the submit button? If so, then it automatically generates an ID on the client side. If that's the case, try something like:

   $('#<%= SubmitBtn.ClientID%>').click(function () { 
       return confirm("Are you sure you want to submit?");
   }); 

EDIT: Just saw that this is an external file, so the above solution won't work. If you are using and ASP.NET button, you can always define a function and have it execute on the onClick event like so:

<asp:Button id="SubmitBtn" onClientClick="myFunction()" runat="server"/>

And then in the javascript file:

function myFunction() {
    return confirm("Are you sure you want to submit?");
}

3 Comments

Ah didn't see see that it was an external file! I'll edit my answer to reflect that
This updated solution does not work either. I've been working on the same issue. I just ran it in the chrome debugger and the function returns false, but the action still happens. If you move it inline on the onClientClick then it works correctly.
If I'm understanding you correctly, then you want your javascript function to prevent a serverside postback? If that is the case and your function has return false;, you will need onClientClick="return myFunction();".

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.