views:

518

answers:

2

Hi, I have a problem with a data collection wizard.

My wizard has a number of client side validators, (regex, required field etc.) and they all work fine.

I have just added a CustomValidator to one of the controls, that is calling some server side code. (unfortunaltely it does have to be server side).

My problem is that this code seems to be called after the wizard has moved on to the next step (or is in the process of moving).

Thus, the fact that it returns false for the validation is of little use to me...

I am adding the Validator dynamically as part of my InitControl method as follows:

mustUploadAnImageValidator = new CustomValidator();
mustUploadAnImageValidator.ControlToValidate = radioButtonList.ID;
mustUploadAnImageValidator.ValidationGroup = "wizardGroup";
mustUploadAnImageValidator.ErrorMessage = "You must select a valid gallery image to use";
mustUploadAnImageValidator.Display = ValidatorDisplay.Static;
mustUploadAnImageValidator.ServerValidate += 
    new ServerValidateEventHandler(mustUploadAnImageValidator_ServerValidate);
mustUploadAnImageValidator.CssClass = "galleryValidationMessageTop";

This works for all other validators, but I can't figure out why the wizard is moving on before my server validation returns.

I've added a javaScript pop-up, called from the server side method, which basically says "I'm not valid" & this appears just after the screen moves on.

Any suggestions gratefully recieved.

A: 

Make sure you're adding the control on every postback, in the Init event.

Edit

You also have to manually wire up the client side validation for custom validators.

ScottE
EvilAndyB
I don't see any client validation set up?
ScottE
Sorry, not making myself clear - there is no Client validation set up for the mustUploadAnImageValidator control, but there is a requiredFieldValidator on the radioButtonList. This validator is working fine, whereas the CustomValidator isn't. It's my understanding that the requiredFieldValidator produces some client side scripting for you?
EvilAndyB
Did you mean the custom validator? A custom validator does not add the client side script for you - you have to wire this up yourself.
ScottE
Hi, I'm aware that you need to add your own client side scripting for a custom validator. I don't want this though. I'm only interested in the server side validation. I was just pointing out that there was a requiredFieldValidator set up at the same time in the InitControl method that was working fine.You said to check it was being added after every postback. Thus as it is added at the same time as a working validator, I don't think this is the problem.
EvilAndyB
A: 

I have managed to sort this out.

I have added an event handler for the NextButtonClick & PreviousButtonClick that simply calls the page validation again.

protected virtual void DataCollectionWizard_PreviousButtonClick(object sender, WizardNavigationEventArgs e)
    {
        //manually validate the page, as the automatic validation seems to ignore CustomValidators
        Page.Validate("wizardGroup");

        if (!Page.IsValid)
        {
            e.Cancel = true;
        }
    }

My server side validator is now being called twice, once by the original wizard code, which is then ignored, and a 2nd time by this method, which then stops the wizard moving on to the next step....

It's not the best fix in the world, but at least it's working! I'll update this post if I ever figure out what is actually going on.

EvilAndyB
"I'll update...if I ever figure out what is actually going on." Please do!
Beska