asp.net中html复选框的自定义验证

asp.net中html复选框的自定义验证,asp.net,validation,Asp.net,Validation,如何在asp.net中验证html复选框? 我试过以下方法 <input type="checkbox" id="chk_rule" class="checkbox-custom" name="chk_rule" runat="server"/> <label for="bodycontent_chk_rule" class="checkbox-custom-label">I have read and agree to the official rules</

如何在asp.net中验证html复选框?
我试过以下方法

<input type="checkbox"  id="chk_rule" class="checkbox-custom" name="chk_rule"  runat="server"/>
<label for="bodycontent_chk_rule" class="checkbox-custom-label">I have read and agree to the official rules</label>
<asp:CustomValidator ID="Agreecheck" runat="server" Display="Dynamic" ForeColor="Red" ErrorMessage="You have to agree the rules." ControlToValidate="chk_rule" OnServerValidate="Agreecheck_ServerValidate"></asp:CustomValidator>
 protected void Agreecheck_ServerValidate(object source, ServerValidateEventArgs args)
{
    args.IsValid = chk_rule.Checked;
}
但我有以下错误

消息:找不到控件引用的控件id“chk_规则” “Agreecheck”的“ControlToValidate”属性。堆栈跟踪:在 System.Web.UI.WebControl.BaseValidator.CheckControlValidationProperty(字符串 名称,字符串propertyName)位于 System.Web.UI.WebControl.CustomValidator.ControlPropertiesValid()位于 System.Web.UI.WebControl.BaseValidator.OnPreRender(EventArgs e)位于 System.Web.UI.Control.PreRenderRecursiveInternal()位于 System.Web.UI.Control.PreRenderRecursiveInternal()位于 System.Web.UI.Control.PreRenderRecursiveInternal()位于 System.Web.UI.Control.PreRenderRecursiveInternal()位于 System.Web.UI.Control.PreRenderRecursiveInternal()位于 System.Web.UI.Page.ProcessRequestMain(布尔值 IncludeStages前同步点,布尔值IncludeStages后同步点)


如何修复此问题?

您可以使用
CustomValidator
查看复选框是否已选中

<asp:CheckBox ID="CheckBox1" runat="server" Text="Check me please" />

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Checkbox not checked" ClientValidationFunction="validateCheckBox"></asp:CustomValidator>

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

<script type="text/javascript">
    function validateCheckBox(sender, args) {
        args.IsValid = document.getElementById("<%=CheckBox1.ClientID %>").checked;
    }
</script>

函数validateCheckBox(发送方,参数){
args.IsValid=document.getElementById(“”)。已选中;
}

我测试了您的代码,如果您用文本框替换复选框,它就会工作。我认为自定义验证器不适合用于复选框。因此,您可以通过一些jquery代码轻松地解决此问题:

<script>
    $('input[type=submit]').click(function ()
    {
        var clientId = '<%= chk_rule.ClientID %>';
        if (!$('#' + clientId).is(':checked'))
        {
            alert('You must agree the rules');
            return false;
        }
    });
</script>

$('input[type=submit]')。单击(函数()
{
var clientId='';
if(!$('#'+clientId).is(':checked'))
{
警报(“您必须同意规则”);
返回false;
}
});
下面是一段代码片段(仅供参考),您的应用程序需要上述代码


我已阅读并同意官方规定
$('input[type=submit]')。单击(函数()
{
如果(!$('chk_rule')。是('checked'))
{
警报(“您必须同意规则”);
返回false;
}
});

您是否使用母版页?请检查@FarzInKanz我使用母版页。我如何使用服务器端有效功能?现在我使用母版页,需要使用服务器端验证
因为当浏览器禁用javascript时,客户端验证无法工作。没有区别。我在一个带有母版页的页面中测试了这一点。当禁用javascript时,验证是否有效?当禁用javascript时,任何自定义验证程序都不起作用。它们通过javascript工作。如果没有javascript,您只能签入“代码隐藏”复选框是否选中。