jqueryasp.net

UseSubmitBehavior="false" and binding OnClientClick?


<asp:Button ID="RecoverButton" runat="server" Text="Recover"
    OnClick="RecoverButton_OnClick" UseSubmitBehavior="false" />

I kind of figured out UseSubmitBehavior="false" overwrites the OnClientClick function I was providing.

I went with this then:

$(document).ready(function() {
    $("[id$=RecoverButton]").live("click", RecoverButton_OnClick);
});

function RecoverButton_OnClick(s, e) {
    var input = $("[id$=MasterUsername]").val();
    var valid = $dn.s.MailValidation(input);

    if (!valid) {
        // prevent postback here
    }
}

But I don't know how I can prevent the postback event from firing up. I tried doing something like e.preventDefault() but it didn't work, ideas?

Update thing is both the __doPostback and jQuery's click handler seem to fire up at the same time (or maybe ASP.NET's fires first), but I placed an alert on my RecoverButton function and firebug already shows the postback happening while the alert is there, blocking execution.


Solution

  • Try this:

    <asp:Button ID="RecoverButton" runat="server" Text="Recover" UseSubmitBehavior="false" />
    
    $(document).ready(function() {
        $("[id='<%= RecoverButton.ClientID %>']").live("click", RecoverButton_OnClick);
    }
    function RecoverButton_OnClick(event) {
        var input = $("[id$=MasterUsername]").val();
        var valid = $dn.s.MailValidation(input);
        if (!valid) {
            return false; //This cancels the submit event
        }
    }