自定义验证器未触发(没有错误消息,但没有验证)

发布于 2024-10-12 09:21:41 字数 1253 浏览 2 评论 0原文

我有一个自定义验证器:

 <asp:CustomValidator ID="QuestionValidator" runat="server" ErrorMessage="Please select an option" ClientValidationFunction="QuestionValidator_ServerValidate" OnServerValidate="QuestionValidator_ServerValidate" ValidateEmptyText="true"></asp:CustomValidator> 

我有一个数据列表提供的问题列表,我需要确保用户选择每个问题的答案。然后,我有了验证功能:

Protected Sub QuestionValidator_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
    Dim SelectedItem As Boolean = False
    For Each c As Control In Page.Master.FindControl("form1").Controls
        If TypeOf c Is RadioButton Then
            Dim rb As RadioButton = DirectCast(c, RadioButton)
            If rb.GroupName = "AnswerOptions" AndAlso rb.Checked = True Then
                SelectedItem = True
            End If
        End If
    Next
    args.IsValid = SelectedItem
End Sub

<script type="text/javascript" language="javascript">  
   function QuestionValidator_ServerValidate() {   
        return true;   
    }   
</script>  

当我运行页面时,没有验证,也没有错误消息。请您指出我哪里出错了,我怀疑它在 Page.Master.FindControl("form1").Controls 处。

我之前曾通过 form1.controls 循环遍历控件来完成此类验证,但这不可用,因为页面使用通过母版页传递的表单。

I have a custom validator:

 <asp:CustomValidator ID="QuestionValidator" runat="server" ErrorMessage="Please select an option" ClientValidationFunction="QuestionValidator_ServerValidate" OnServerValidate="QuestionValidator_ServerValidate" ValidateEmptyText="true"></asp:CustomValidator> 

I have a list of questions provided by a datalist, which I need to ensure a user selects an answer to each question. I then have my validation functions:

Protected Sub QuestionValidator_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
    Dim SelectedItem As Boolean = False
    For Each c As Control In Page.Master.FindControl("form1").Controls
        If TypeOf c Is RadioButton Then
            Dim rb As RadioButton = DirectCast(c, RadioButton)
            If rb.GroupName = "AnswerOptions" AndAlso rb.Checked = True Then
                SelectedItem = True
            End If
        End If
    Next
    args.IsValid = SelectedItem
End Sub

<script type="text/javascript" language="javascript">  
   function QuestionValidator_ServerValidate() {   
        return true;   
    }   
</script>  

When I run the page, there is no validation and no error message. Please can you point out where I am going wrong, I'm suspicious it is at Page.Master.FindControl("form1").Controls.

I have previously done such validation by looping through controls via form1.controls but this is unavailable as the page uses a form passed down via the master page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清君侧 2024-10-19 09:21:41

您的代码中可能存在缺陷。因为按照逻辑,如果页面上有 50 个单选按钮,即使只选择了一个,您的验证也会通过。此外,每个项目的所有 RB 的组名都是相同的。不确定 ASP.NET 是否会重命名这些内容,如果没有,则所有问题的所有 RB 都会归为一类。

对于循环部分,您可以循环遍历 DataList.Items 集合,而不是循环遍历表单上的所有控件:
暗淡项目作为 DataListItem

For Each item In  DataList1.Items
   Dim ctrl As Control
   For Each ctrl In  item.Controls
         'do your rb state check here       
   Next ctrl
Next item

There could be a flaw in your code. Because as per the logic if out of 50 radio buttons on your page even if just one is selected your validation will pass. Also the groupname for all the RBs is same for each item. Not sure if ASP.NET renames those and if not then all RBs with be grouped into one for all the questions.

For the looping part you can loop through DataList.Items collection instead of looping through all controls on the form:
Dim item As DataListItem

For Each item In  DataList1.Items
   Dim ctrl As Control
   For Each ctrl In  item.Controls
         'do your rb state check here       
   Next ctrl
Next item
智商已欠费 2024-10-19 09:21:41

我会尝试循环“Page.NamingContainer”或“Page.Controls”,看看效果如何。

I would try looping through either "Page.NamingContainer" or "Page.Controls" and see how that goes.

呆° 2024-10-19 09:21:41

也许我错过了一些东西,但您不应该在自定义验证器中设置属性 ControlToValidate="ID_of_the_control_to_validate" 吗?

Maybe I missed something, but shouldn't You also set property ControlToValidate="ID_of_the_control_to_validate" in the custom validator?

深海里的那抹蓝 2024-10-19 09:21:41

我认为您需要为 CustomValidator 提供 ValidationGroup 以及 Validaiton 过程中涉及的所有控件

I think you need to give ValidationGroup for the CustomValidator and all the Controls Involved in the Validaiton process

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文