12088

RequiredFieldValidator and preventing CausesValidation

Question:

I've got a <a href="http://msdn.microsoft.com/en-us/library/5hbw267h%28v=vs.80%29.aspx" rel="nofollow">RequiredFieldValidator</a> on a contact form.

It works as intended when people click 'Submit', but if they click 'Cancel' or any of the multiple menus on my form, the <a href="http://msdn.microsoft.com/en-us/library/5hbw267h%28v=vs.80%29.aspx" rel="nofollow">RequiredFieldValidator</a> cancels the action.

I have already searched and found that I need to set the other controls on my form using <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation.aspx" rel="nofollow">CausesValidation</a> = <strong>False</strong> (using <a href="https://stackoverflow.com/questions/2915530/bypassing-a-requiredfieldvalidator" rel="nofollow">this</a> post), but do I have to do that for every control on my page?

What makes it worse is that the menus on my form are contained in a Master.Page, and they are mostly <DIV> style CSS buttons, but clicking any of the buttons causes the <a href="http://msdn.microsoft.com/en-us/library/5hbw267h%28v=vs.80%29.aspx" rel="nofollow">RequiredFieldValidator</a> to fire and fail the form.

Shouldn't the default be False and I have to turn on which control sets the validation?

Answer1:

you can set validation groups

<asp:TextBox ID="tb1" runat="server" ValidationGroup="ValidateMe" /> <asp:TextBox ID="tb2" runat="server" /> <asp:RequiredFieldValidator" ID="rfv1" runat="server" ControlToValidate="tb1" ValidationGroup="ValidateMe" /> ... <asp:Button ID="btnSubmit" runat="server" ValidationGroup="ValidateMe" />

came from <a href="https://stackoverflow.com/questions/1636200/asp-validation-off-by-default" rel="nofollow">here</a>

Edit , sorry I didn't put put this in code properly and it didn't display:

Or you can always use and handle those on client side if they are just cancel and stuff like that

<input type="button" >

Answer2:

You only need to set:

CausesValidation = False

To your buttons performing an action

As an alternative, you could add a ValidationGroup attribute to your controls and buttons to control <em>which buttons raise the validation</em>. Only the controls matching the ValidationGroup specified will be validated

Recommend

  • recursive variadic template can't deduce argument
  • StringSplitOptions.RemoveEmptyEntries equivalent for TextFieldParser
  • ANTLR generating empty conditions
  • Getting random data from a MySQL database but not repeating data
  • Android: playing audio files in /res/raw by file name
  • Writing Unittest for generic classes… best approach?
  • FluentMigrator Failed Migrations Don't Rollback?
  • Microsoft Chart Controls for Microsoft .NET Framework 4.0
  • Mixing WebForms and MVC: What should I do with the MasterPage?
  • How to get links to open in the native browser in iOS Meteor apps?
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Highlight one bar in a series in highcharts?
  • FileReader+canvas image loading problem
  • Ajax Loaded meta Tags
  • Paperclip, set path outside of rails root folder
  • DomPDF {PAGE_NUM} not on first page
  • Insert into database using onclick function
  • Deselecting radio buttons while keeping the View Model in synch
  • Nant, Vault & Windows Integrated Authentication
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Bug in WPF DataGrid
  • Modifying destination and filename of gulp-svg-sprite
  • javascript inside java/jsp code
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • jQuery tmpl and DataLink beta
  • Web-crawler for facebook in python
  • How can I estimate amount of memory left with calling System.gc()?
  • GridView Sorting works once only
  • How do I rollback to a specific git commit
  • A cron job substitute?
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal