Click here to Skip to main content
15,916,463 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi, I'm having trouble solving a problem in asp.net.
I have a web application that have four buttons which is link to a Sql server database where I can Add, Get, Update, Delete clients. I have set CausesValidation to true for the Add button and the Update button on all textboxes (ID, FirstName, LastName, Email, Confirm Email). However, each time I retrieve a client and try to update some of the fields, the confirm email validation comes up with the RequiredFieldValidator error "Confirmation email required!". My Confirm Email consist of two validation, a RequiredFieldValidator and a CompareValidator.
How can I disable this RequiredFieldValidator error from popping up for the Update button only?
My database consist of ID, FirstName, LastName, Email. Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 27-Aug-14 11:31am    
Stop requiring validation... :-)
—SA
Member 10703465 27-Aug-14 11:43am    
Thanks, but I do need the Require validation for my Add button. If I remove it I can add any clients without confirming their email.

Change your Required Field Validator on that control to a custom validator. You can then either validate on the back-end, depending on which button was pressed or, better, add a custom JavaScript validation function. If you then use a page global flag which you set using a Javascript OnClientClick function on your update button. You can then check this flag in your custom validation function, returning true when set and checking your text box if not (returning the appropriate state.)

Note that you can still validate on both client and server side if that is required.
 
Share this answer
 
Comments
Member 10703465 27-Aug-14 12:20pm    
Thanks PhilLenoir, I added a Custom validator and its working find :)
Typically, if you need different sets of validation for different buttons on the same page, the simplest solution is to use validation groups:
http://msdn.microsoft.com/en-us/library/ms227424%28v=vs.100%29.aspx[^]

Basically, you would choose a meaningful group name, and set it as the ValidationGroup on the validator controls and the buttons which should trigger the validators in that group.

However, you would need to duplicate the validator controls which apply to both groups.

ASP.NET
<asp:TextBox id="Email" runat="server" ... />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Email" ValidationGroup="Add" ... />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Email" ValidationGroup="Update" ... />

<asp:TextBox id="ConfirmEmail" runat="server" ... />
<asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmEmail" ValidationGroup="Add" ... />
<%-- NB: Not required for the "Update" group --%>

<asp:Button runat="server" Text="Add" ValidationGroup="Add" ... />
<asp:Button runat="server" Text="Update" ValidationGroup="Update" ... />
 
Share this answer
 
Comments
Member 10703465 27-Aug-14 19:16pm    
Totally agree with your solution Richard, and it's a lot easier.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900