【问题标题】:Customvalidation for html checkbox in asp.netasp.net中html复选框的自定义验证
【发布时间】:2017-02-04 12:12:51
【问题描述】:

如何验证 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;
}

但是我遇到了以下错误。

消息:无法找到由 'Agreecheck' 的 'ControlToValidate' 属性。堆栈跟踪:在 System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(字符串 名称,字符串属性名称)在 System.Web.UI.WebControls.CustomValidator.ControlPropertiesValid() 在 System.Web.UI.WebControls.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(布尔值 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint)

我该如何解决这个问题?

【问题讨论】:

  • 您使用母版页吗?
  • 请查看link
  • @FarzInKanz 我使用母版页。

标签: asp.net validation


【解决方案1】:

我测试了你的代码,如果你用文本框替换复选框,它就可以工作。我认为自定义验证器不适用于复选框。所以你可以通过一些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>

这是一个代码sn-p(仅供参考),你的应用需要上面的代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
<input type="submit" value="submit" id="btnSubmit">
<script>
        $('input[type=submit]').click(function ()
        {
            if (!$('#chk_rule').is(':checked'))
            {
                alert('You must agree the rules');
                return false;
            }
        });
</script>

【讨论】:

  • 现在我使用母版页,必须使用服务器端验证。
    因为当浏览器禁用 javascript 时,客户端验证无法工作。
  • 没有差异。我在带有母版页的页面中对此进行了测试。
  • 当禁用 javascript 时,验证是否有效?
  • 禁用 javascript 时,任何自定义验证器都不起作用。他们通过 javascript 工作。
  • 如果没有javascript,您只能签入后面的代码是否选中了复选框。
【解决方案2】:

您可以使用CustomValidator 来查看 CheckBox 是否被选中。

<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>

【讨论】:

  • 如何使用服务端有效函数?
猜你喜欢
  • 2011-07-24
  • 2019-01-08
  • 1970-01-01
  • 1970-01-01
  • 2016-02-23
  • 2014-12-04
  • 2015-04-17
  • 1970-01-01
  • 2012-06-20
相关资源
最近更新 更多