在 asp.net c# 中禁用 RequiredFieldValidator

Disable RequiredFieldValidator in asp.net c#

我正在尝试通过单选按钮列表值禁用 RequiredFieldValidator。我已经对它进行了编码,但它似乎没有用。

到目前为止我做了什么:

protected void radioButtonList(object sender, EventArgs e)
        {
  if (((RadioButtonList)dvInsertPromotion.FindControl("RadioButtonList2")).SelectedValue == "Y")
            {
                ((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Enabled = false;
                ((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate")).Enabled = false;
                this.addPromotion.Show();
            }
            else
            {
                ((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Enabled = true;
                ((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate")).Enabled = true;
                this.addPromotion.Show();
            }
}

html:

 <asp:RadioButtonList ID="RadioButtonList2" runat="server" ValidationGroup="addValidationGp" OnSelectedIndexChanged="radioButtonList">
                                     <asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
                                     <asp:ListItem Text="No" Value="N" Selected></asp:ListItem>
</asp:RadioButtonList>
                                    <asp:TextBox ID="txtPubDate" Width="75"  MaxLength="10" runat="server" AutoPostBack="true" OnTextChanged="insertStartEndDateTime_SelectedIndexChanged"/>
                                <asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtPubDate"
                                    PopupPosition="Right" Format="dd/MM/yyyy" />
                                <asp:RequiredFieldValidator ID="rfvdate" ValidationGroup="addValidationGp" runat="server"
                                    ControlToValidate="txtPubDate" ErrorMessage="*" Display="Dynamic" Enabled="true"
                                    SetFocusOnError="true" /><br />

我过去遇到过同样的问题,唯一我能够解决这个问题的方法是让验证器完全不可见:

 ((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Visible = false;

只要您想要启用它们,请不要忘记再次将它们设置为可见。