简体   繁体   中英

C# Enable and Disable Validators on Web Form

I am doing a customer registration form which allows new customers to fill in. I've put requirefieldvalidator every field and comparevalidator for comparingt the password and confirmpassword. I have submit button and back button. The problem i encountered is no matter what button i clicked, submit button or back button, if i don't fill in any of the requirement i will not proceed. This is fine to the submit button as it needs to check whether every detail is filled in, but for back button, i want to allow customers to go back to previous page by clicking this button which validators are not needed.

Is there any way to control the validators?

use ValidationGroup in all validation with same group name and give same group name for button also

Exapmle

<asp:TextBox runat="server" ID="tb1"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfv1" ControlToValidate="tb1" ErrorMessage="*" ValidationGroup="gvSave">
</asp:RequiredFieldValidator>

<asp:TextBox runat="server" ID="tb2" Visible="false"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfv2" ControlToValidate="tb2" ErrorMessage="*" Enabled="false" ValidationGroup="gvSave">
</asp:RequiredFieldValidator>

<asp:Button runat="server" ID="btn1" Text="Save" onclick="btn1_Click" ValidationGroup="gvSave"/>
<asp:Button runat="server" ID="btn2" Text="Show" onclick="btn2_Click" />

Here

when you click btn1 it check for validation and

for button btn2 will not check for validation

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