Link to home
Start Free TrialLog in
Avatar of kdunnett
kdunnett

asked on

Validation control exist on CodeBehind with no ControlToValidate?

Hey all,

Quick question.  I want to create a validation control on the codebehind, but don't want it associated to anything.  Want it more like a flag, but want access to the Page.IsValid stuff.

Basically, I have a tonne of validation happening now, but have some extra stuff going on, but need a flag for it.  But its all server side stuff (doing db calls, some business logic, etc), that has no controls or anything.

But I want this validation control to have the IsValid stuff, so that I can simply check it in the validation routine (is true or false).

I kind of have it, but having an issue.  I created a RequireFieldValidator via:
RequiredFieldValidator rfvInitator = new RequiredFieldValidator();

And its on the codebehind, and then assoicated within a method, and forcing:
rfvInitator = true / rfvInitator = false.... depend on the results.

Now, when I go to the validation button, the page doesn't seem to reconize this.  I'm assuming its cause its not associated to a control of any type.  If theres a way to do this programmically, great, but would rather not have a hidden textbox and a validation control on that (all hidden), to get this to work...  Shouldn't there be something better?  Or am I smokin' something?

Cheers all,
Kris
ASKER CERTIFIED SOLUTION
Avatar of mrichmon
mrichmon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrichmon
mrichmon

Notice that the validator is not associated with any field like you asked

But if you do have a validation summary on the page, the message that you pass in to the validator DOES get added to that validation summary.
you could still assign the validator to some control .. u could have a custom validator attached to some control .. have a dummy function defined on the web page (aspx) which does not do anything ..
Avatar of kdunnett

ASKER

mrichmon,

When I use your function
Public static void AddCustomValidator(System.Web.UI.Page Webpage, string errorMsg)
{
     CustomValidator MyValidator = new CustomValidator();
     MyValidator.ErrorMessage = errorMsg;
     MyValidator.IsValid = false;
     Webpage.Validators.Add(MyValidator);
}

I can't seem to get the CustomVaidator to be anyting other then IsValid = true, no matter what I do.
mrichmon,

I should explain...  I have the below code at the beginning of my validation code.  Basically, its code at a later date that changes the isValid to true.

CustomValidator MyValidator = new CustomValidator();                  
MyValidator.IsValid = false;
Page.Validators.Add(MyValidator);

But even if I comment out that code that makes MyValidator.IsValid = true, it still acts like its true... Even though I made it false.

Ideas?
Kris
Got it... My mistake!

Tahnks all...
Kris
Just got the emails saying there were updates to this thread - glad you figured it out.