ASP.NET - RequiredFieldValidator to validate only particular controls

Mikayil Abdullayev picture Mikayil Abdullayev · Apr 16, 2012 · Viewed 14.2k times · Source

I have a web form. There are many different sections. I can say that each section displays data of a datatable. At each section I have OK and Cancel buttons. When I press OK any changes to the table in the database takes place. I've also put some Requiredfieldvalidators. Let's say I'm inserting a new record in the section one and the fields are correctly typed. When I press OK I get error message raised by the rest of the validators that are on the other sections. Isn't there any way that when I press OK button of a particular section to get validation errors of that same area? So what I probably need is a button that will not serve as the hole page submitter but rather a submitter of a specific section.

Answer

janhartmann picture janhartmann · Apr 16, 2012

Place a ValidationGroup on the RequiredFieldValidator's. Then place the same ValidationGroup on the correct submit button. When its clicked, only validation controls that are a part of the group are validated.

http://msdn.microsoft.com/en-us/library/ms227424.aspx