Validation Summary - Adding Error Messages

L

Lucas Tam

Hi all,


I posted a message a couple minutes early and I'm not sure if it was too
clear.

I'm interested in adding custom messages to the validation control - is
there an easy way to do this?

Here is what I'm intending to do:

1. Have a form, validate all fields (done)
2. If Page is valid insert into database (done)
3. If there is a database error, display an error in the
ValidationSummary

I'm having trouble with #3. If I build a "database" custom validator on
the page, it seems I need to build the Customer Validator event handler
in order to make the error message display (the event handler in this
case return a false every time to trigger the error message). Not hard -
but why bother if there is ane easier method. I can't seem to find a way
to trigger an error (i.e. with just customvalidator.isvalid = false).

I need a simple way of adding error messages. Does anyone have any
suggestions?

Thanks.
 
A

Andrey

3. If there is a database error, display an error in the
ValidationSummary

I'm having trouble with #3. If I build a "database" custom validator on
the page, it seems I need to build the Customer Validator event handler
in order to make the error message display (the event handler in this
case return a false every time to trigger the error message). Not hard -
but why bother if there is ane easier method. I can't seem to find a way
to trigger an error (i.e. with just customvalidator.isvalid = false).

I need a simple way of adding error messages. Does anyone have any
suggestions?

Thanks.

I could not find any way to add messages to ValidationSummary, however
I came up with a way around it:
1. create asp textbox and use stylesheet to make it hidden
2. put some text in it (otherwise validation will not be fired)
3. create custom validator for this hidden text box
4. create class-level private variable and assign it NULL
private string errorMessage = null;
5. in your validator, put code:
if (errorMessage != null) {
args.IsValid = false;
customValidatorControl.ErrorMessage = errorMessage;
}
6. when you saving/updating database, do this
try {
... do database or other things here ...
} catch (Exception e) {
errorMessage = e.Message;
customValidatorControl.Validate();
}

Now you are triggering validation for the second time, and this time
it will fail because errorMessage is not null. This will cause
ValidationSummary to display your custom message.

Andrey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top