1

The following does not work. Have a server submit button and trying to invoke the click action of the button on client side to so some validations. However, the function does not get invoked!

<head runat="server">
<script type="text/javascript" src="scripts/jQuery.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button runat="server" Text="Next" ID="btnNext" />
    </div>
    </form>
</body>

$(document).ready(function() {
$('#<%= btnNext.ClientID %>').click(function() {
    alert("Success");
});
});

Any help is appreciated.

5
  • Are you sure the jquery.js file is loaded (I mean no 404 error)? The code looks fine.. Commented Jul 18, 2012 at 23:49
  • Are you invoking the click on the server or with some other client-side javascript? Commented Jul 18, 2012 at 23:49
  • what does the html in the browser look like when you do view source? It could be that something isn't rendering as expected. Commented Jul 18, 2012 at 23:54
  • Please can you tell us what does your code to do with the header tag having a runat=server in it Commented Jul 18, 2012 at 23:56
  • no error, I had created a new web project to just test this piece and maybe that's how I still have the runat=server in head. but, does that affect my code? Commented Jul 19, 2012 at 16:35

3 Answers 3

1

If you're calling btnNext_Click, that code runs server-side. So the button's click event never gets triggered client-side.

If you want to trigger it client-side, you will need to use client-side script, eg:

if (<%= some_condition %>)
{
    $('#<%= btnNext.ClientID %>').click();
}
Sign up to request clarification or add additional context in comments.

2 Comments

isn't that supposed to happen automatically like a radiobutton.change or a keypress event? why do we explicitly have to call this event when I have so many other events firing automatically on the same page with no issues?
@asa: ASP blurs the line between code that runs server-side and code that runs client-side, which can make it confusing. Usually a client-side click executes server-side code, not the other way around (like you're doing). I think you're going to have to click() your button manually using javascript (as above). If you find another way, let me know. ;)
1

try this

<asp:Button runat="server" Text="Next" ID="btnNext" onClientClick="fnClickEvent();"/>

then in client code

    function fnClickEvent(){
        var valid = false;
        if (//validation code---){
             valid = true;
                } 
        if valid{ return true; }
        if notvalid { return false;} \\\ this prevents postback

         }

3 Comments

I just thought of this.. but, I am still bewildered as to why the event was not fired automatically on the button click like the radio.change that I have and working perfectly.
<asp:Button > act differently then <input type="button" > , i personally always use the second type , it is a common problem as you can see by googling "stop asp button from postback"
this also works, of course! just wanted to know why my code doesn't.
0

For some weird reason this particular "click" event runs when it is put inline in the aspx page itself. The moment I save it to the js file, it stops working. Keep in mind I have other controls on the page that have events in the same js file that works perfect.

Comments

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.