【问题标题】:How to validate a CheckBox with CustomValidator within a RadListView如何在 RadListView 中使用 CustomValidator 验证 CheckBox
【发布时间】:2019-09-18 13:59:12
【问题描述】:

我有一个Telerik:RadListView 控件和多个复选框。我需要在提交表单之前验证这些复选框。 我尝试了带有ControlToValidate 属性的内置验证器,但它给了我错误Control 'chkQuestion' referenced by the ControlToValidate property of 'chkQuestionValidator' cannot be validated.,这是非常合乎逻辑的,因为它们都在RadListView 中。

之后,我创建了一个 CustomValidator;

<asp:CheckBox ID="chkQuestion" 
CssClass="AcceptedAgreement"
 Visible="true" 
runat="server" 
Text=" I confirm" 
AutoPostBack="false" />

<asp:CustomValidator 
ClientValidationFunction="CheckBoxRequired_ClientValidate" 
EnableClientScript="true" 
ID="chkQuestionValidator" 
runat="server" 
Enabled="true" 
SetFocusOnError="true" 
ErrorMessage="Please confirm" 
ForeColor="Red" />

这些都在Telerik:RadListView 中,这意味着我有多个,但我无法在我的客户端自定义验证器函数中链接它们。

这是我的验证器函数:

   function CheckBoxRequired_ClientValidate(sender, e) {
           var c = jQuery(".AcceptedAgreement input:checkbox");
           var chb = validator

           if (c.is(':checked')) {
               e.IsValid = true;
           } else {
               e.IsValid = false;
           }
       }

但使用此功能,我无法单独验证它们。 when one of the checkboxes checked, this function act like all of them are checked.这是因为它们都有相同的 css 类,这就是我坚持的重点。

如果你能帮助我,我们将不胜感激。

【问题讨论】:

    标签: javascript c# asp.net webforms


    【解决方案1】:

    对于那些来这里寻求解决方案的人,这里是:

            function handleSubmitForm(sender, args) {
                var c = jQuery(".AcceptedAgreement input:checkbox");
                c.each(function (index) {
                    if (!c[index].checked) {
                        args.set_cancel(true);
                    }
                });
                args.set_cancel(true);
            }
    
    

    基本上我将这段代码分配给我的按钮,在回发之前调用它,如果它不符合我的条件,它会停止回发。你可以在 if 子句中做任何你想做的事情。

    你可以调用这个函数:

    <telerik:RadButton ID="btnSubmit" 
    OnClientClicking="handleSubmitForm"
    Text="Submit" OnClick="btnSubmit_Click" />
    

    【讨论】:

      猜你喜欢
      • 2018-10-20
      • 1970-01-01
      • 2012-07-26
      • 2011-07-03
      • 1970-01-01
      • 2018-11-28
      • 2023-03-23
      • 2012-05-26
      • 2011-01-19
      相关资源
      最近更新 更多