设置字段以有条件地使用必填字段验证器

Setting fields to conditionally use required field validator

I have a radio button with 2 options, when 1 is selected I want to require 5 text boxes to be filled out, when 2 is selected those text boxes don't have to be.我在 asp.net 中使用 C# 代码,到目前为止,我在必填字段上使用 asp requiredfieldvalidators。但我需要帮助来设置处理此请求的逻辑。我有 C 的背景,但脚本对我来说仍然很陌生,所以如果有解决方案,请告诉我!在此先感谢您的帮助。

使用 CustomValidator - 它甚至可以让您选择验证客户端站点(java 脚本)或服务器端(代码隐藏)

示例客户端验证 ASPX:

<asp:Button runat="server" ID="btncbValid" Text="Test" ValidationGroup="vg1" />
<asp:CustomValidator ID="cvValidator"  ValidationGroup="vg1" runat="server" ErrorMessage="Error!"
ClientValidationFunction="validateMethod"></asp:CustomValidator>

JS:

function validateMethod(sender, args) {
var txt1 = document.getElementById("<%= tb1.ClientID %>");
var txt2 = document.getElementById("<%= tb2.ClientID %>");
var txt3 = document.getElementById("<%= tb3.ClientID %>");
var txt4 = document.getElementById("<%= tb4.ClientID %>");
var txt5 = document.getElementById("<%= tb5.ClientID %>");

var rb1 = document.getElementById('<%= rb1.ClientID %>');
var rb2 = document.getElementById('<%= rb2.ClientID %>');

args.IsValid = 
(
(rb1.checked && txt1.value != "" && txt2.value != "" && txt3.value != "" && txt4.value != "" && txt5.value != "" )
||
(rb2.checked && txt1.value == "" && txt2.value == "" && txt3.value == "" && txt4.value == "" && txt5.value == "" )
)
}