简体   繁体   中英

OnClientClick preventing OnClick event?

I have a button, I need to fire an onclick event, and an onclientclick event. The onclientclick would be something like this:

(function($){
$.fn.validate = function()  {
    var option = $('#<%=LblWarnings.ClientID%>').html();   
        if (option == "option1_OK" ||
            option == "option1_OK")  
            alert(wrong);
        else {
            $('#<%=LblWarnings.ClientID%>').html("Incomplete");
            alert(wrong);}        
    };
})(jQuery);

Validating function, checks the text of a label, if condition's true it should go on and execute the OnClick event. And if it's false, it should NOT FIRE the code behind (method called WriteInfo in my .cs file, but just update a label so the user knows something was wrong while he was submitting data.

Is it possible for the onclientclick to stop the onclick event? If so, could you help me providing me the asp:button sentence so I can trigger both events.

Thanks.

After trying Adil Answer, I get an error "object expected"

Maybe there's something wrong the way I'm calling the function?

<asp:Button ID="ButtonRadioValue" CssClass="customButton" runat="server" onclick="WriteInfo" OnClientClick="validate();" Text="Accept" />

You can use stopPropagation() method to stop postback of return false.

Use stopPropagation();

 (function($){
    $.fn.validate = function(event)  {
        var option = $('#<%=LblWarnings.ClientID%>').html();   
            if (option == "option1_OK" ||
                option == "option1_OK")  
                alert(wrong);
                event.stopPropagation();
            else {
                $('#<%=LblWarnings.ClientID%>').html("Incomplete");
                alert(wrong);}        
        };
    })(jQuery);

Using return false;

(function($){
$.fn.validate = function()  {
    var option = $('#<%=LblWarnings.ClientID%>').html();   
        if (option == "option1_OK" ||
            option == "option1_OK")  
            alert(wrong);
            return false;
        else {
            $('#<%=LblWarnings.ClientID%>').html("Incomplete");
            alert(wrong);}        
    };
})(jQuery);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM