Validation Controls

  • Thread starter Thread starter Julián Sanz García
  • Start date Start date
J

Julián Sanz García

Hi,

In my asp.net page, i have 2 linkbuttons and 4 textboxes. Each textbox have
a RequieredFieldValidator. I want to do that when i push one of the
linkbuttons it executes 2 RequieredFieldValidator but not the other 2, and
when i push the other linkbutton it executes the other two
RequieredFieldValidator.

Is this possible?

Thanks
 
Sure, it's possible.

You can control whether a validator will execute through the Enabled
property. You will also want to control when the validation happens, so you
will want to set the LinkButton's CausesValidation property to false. Then,
you enable the validators you want before you call Page.Validate( ) which
will fire the validators that are enabled. So the code for the LinkButton
might look something like this:

protected void LinkButton_OnClick(object sender, System.EventArgs e)
{
Validator1.Enabled = true;
Validator2.Enabled = true;
Validator3.Enabled = false;
Validator4.Enabled = false;
Page.Validate( );
if (Page.IsValid)
{
//Perform action
}
}
 
It will show the error messages provided you either have a ValidationSummary
control on the page or you have the Validation control's Display property
set to something other than "None".

Also, it is important that you not redirect from the page, and you will need
to make sure that you check whether the page is valid before performing any
operations, hence why I included the if (Page.IsValid) part of the code.
 
Back
Top