2

I want to call Javascript function from Check box (Only client side), i tried following way but it is not working. can any one give answer?

<asp:CheckBox ID="chkAdd" runat="server"  OnClientClick="return validation();" OnCheckedChanged="btnAddMultiple_Click" AutoPostBack="true" />

Thanks

6
  • Have you checked your error console for JavaScript errors? Commented Feb 12, 2014 at 14:47
  • Are you getting an error? Have you made sure that the script is included in the page properly? Dev tools will help answer these questions. Commented Feb 12, 2014 at 14:48
  • Remove AutoPostBack="true" Commented Feb 12, 2014 at 14:48
  • remove the AutoPostBack="true". That causes an auto postback, as the name says. Commented Feb 12, 2014 at 14:48
  • javascript function is working fine..Thanks Commented Feb 12, 2014 at 14:49

1 Answer 1

5

You want to OnClick event if you want to call javascript function.

<asp:CheckBox ID="chkAdd" runat="server" OnClick="return validation();" 
    OnCheckedChanged="btnAddMultiple_Click" AutoPostBack="True" />
<script type="text/javascript">
    function validation() {
        if (confirm("Are you sure you want to post back?")) {
            __doPostBack('<%= chkAdd.ClientID %>', '');
        } else {
            // Optional: change back the CheckBox to original position
            return false;
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

How will it call the server side event on success?
@IrfanTahirKheli I do not know what the OP's intention. However, when one of the server control post back to server, btnAddMultiple_Click event'll also fire if the checked is changed. You only use AutoPostBack="true" if you want to fire CheckedChanged event immediately.
Please see my updated code which trigger the server side post back based on client side function.
it will not call server side event if you return false from the javascript function. @Win
@IrfanTahirKheli It is my intention. return false means user clicks on Cancel button at confirmation dialog. I agree it is kind of confusing because OP did not say what javascript function is supposed to do. I hope 1612 get the idea.

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.