我有一个自定义验证器:
<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 循环通过控件来完成此类验证,但这不可用,因为页面使用通过母版页传递的表单。