Just Code‎ > ‎

C# Asp.Net - Checkbox validation using custom validator

posted Feb 14, 2010, 3:56 AM by Peter Henell

protected void RegisterCheckBoxValidation()
        {
            ClientScript.RegisterClientScriptBlock(typeof(System.Web.UI.Page), "ClientValidationScript",
            "\n<script language=\"javascript\" type=\"text/javascript\">\n" +
            "function ClientValidationScript(source, args)\n" +
            "{\n" +
            "    args.IsValid = document.getElementById('" + checkbox.ClientID + "').checked;\n" +
            "}\n" +
            "</script>\n");
            
        }
protected void Validator_ServerValidate(object sender, ServerValidateEventArgs svea)
        {
            svea.IsValid = (checkbox.Checked == true);
        }

denna kod p? sidan:

<asp:CheckBox id="checkbox" runat="server" Checked="false" />
            <asp:CustomValidator ID="validator" ClientValidationFunction="ClientValidationScript" OnServerValidate="Validator_ServerValidate"
                Text="*" ErrorMessage="m?ste klicka i"
                ValidationGroup="" runat="server"></asp:CustomValidator>

<!-- Detta l?ggs till l?ngst ner -->
<script type="text/javascript">
        ValidatorHookupControlID('<%= checkbox.ClientID %>',document.getElementById('<%= validator.ClientID %>'));
</script>
Comments