在asp.net中禁用RequiredFieldValidator c#

时间:2016-05-11 11:59:10

标签: c# asp.net requiredfieldvalidator

我试图通过单选按钮列表值禁用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 />

1 个答案:

答案 0 :(得分:1)

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

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

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