About Me

My photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).

Friday, June 02, 2006

Multiple Control Validator

Ok, so I have to come up with a validation control that would validate two textboxes to see if either one has been populated. The custom validator, unfortuntly, only validates if something has been entered into one of the textboxes. To get around this I set up two private boolean values to represent if text has been entered or not.

private bool TxtBox1 = false;
private bool TxtBox2 = false;

I then have my two custom validator functions, which are similar, except for the name of the second validator and it's TxtBox2:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (args.Value == "")
{
TxtBox1 = false;
}
else
{
TxtBox1 = true;
}
args.IsValid = getValidation();
}

So, I just check if there was a value entered and set my private variables.
I then call a function to assign the validation.

private bool getValidation()
{
if (TxtBox1 TxtBox2)
{
return true;
}
else
{
return false;
}
}

Now to deal with the textboxes being both empt

protected void ButtonSubmit_Click(object sender, EventArgs e)
{
this.Page.Validate();
if (TxtBox1 == false && TxtBox2 == false)
{
this.CustomValidator1.IsValid = false;
this.CustomValidator2.IsValid = false;
}
}

First validate the other controls, then validate our custom controls to see if both were left blank.
If so, then set both validators to false.

This is what I came up with so far, I might find a way to improve the summary, so that it would only give one line as the error instead of 2.

No comments: