简体   繁体   English

在asp.net c#中禁用RequiredFieldValidator

[英]Disable RequiredFieldValidator in asp.net c#

I am trying to disable a RequiredFieldValidator by radio button list value.我正在尝试通过单选按钮列表值禁用 RequiredFieldValidator。 I have it coded but it doesn't seems to be working.我已经编码了,但它似乎不起作用。

What i have done so far:到目前为止我做了什么:

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: 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 />

I've had the same problem in the past and the only way I was able to solve this was by making the validators invisible altogether:我过去也遇到过同样的问题,我能够解决这个问题的唯一方法是让验证器完全不可见:

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

Just don't forget to set them to visible again whenever you want to enable them.只是不要忘记在您想要启用它们时再次将它们设置为可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM