简体   繁体   中英

Asp.net validators validate Disabled control

I have a Panel control that contains some control such as text boxes.I want to use asp.net validators to validate text boxes.But if Panle is disabled then text boxes become disabled but validators such as RequiredFieldValidator validate disabled text box.

<asp:Panel ID="Panel1" runat="server" Enabled="false">
    <asp:TextBox ID="TextBox2" runat="server" />
    <asp:RequiredFieldValidator runat="server" ErrorMessage="RequiredFieldValidator"
        ForeColor="#FF3300" ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
</asp:Panel>

How I can set for validators that don't validate disabled controls?

if some control is disabled you can set its property CausesValidation="False"

<asp:Button id="Button1" runat="server"
  Text="Cancel" CausesValidation="False">
</asp:Button>

EDITED

can you do this way

if (!panel.Enabled)
{
    RequiredFieldValidator1.Enabled = false;// disable your all validators
}

Page_Load()中添加此行

RequiredFieldValidator1.Enabled = panel.Enabled;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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