Change ErrorMessage Text Dynamically

J

jdp

Is there a way to set/change the ErrorMessage and/or Text property of
validationcontrols without making a trip to the server? What I have so
far is a usercontrol that inherits from BaseValidator and overrides the
Pre_Render event in which I have javascript code to do validation. I
also override the AddAttributesToRender event to point to my new
validation function. I'm looking to have a central repository of
error messages so each developer doesn't have to figure one out.

Any feedback would be appreciated.

Thanks.
 
B

Brennan Stehling

It sounds like you are mixing a few things together.

First, you appear to be using the Validators the hard way. Why touch
the PreRender event? Simply use the CustomValidator instead of the
BaseValidator and implement the ServerValidate event.

http://msdn2.microsoft.com/en-us/li...bcontrols.customvalidator.servervalidate.aspx

Then you can set the ClientValidationFunction which points to the
Javascript function which runs on the client-side.

http://msdn2.microsoft.com/en-us/li...customvalidator.clientvalidationfunction.aspx

It will automatically use the values from your ErrorMessage and Text
properties.

Also, I believe what you want to do is maintain a common and shared set
of strings to be used across the application. This is done in .NET
using Resource (.resx) files.

http://msdn.microsoft.com/library/d...guide/html/cpconResourcesInResxFileFormat.asp

For the sake of simplicity, I would create a class exposing logically
named Properties which do lookups in the .resx shared file. Then you
just add new entries in your .resx file, wrap it in the helper class
and every class which sets an ErrorMessage would use it. Each
developer does not have to concern themselves with how it is loading
the string, but can easily access it.

Brennan Stehling
http://brennan.offwhite.net/blog/
 
J

jdp

Thanks for the reply and please forgive me newness or simplicity in
these questions...

If I understand what you're saying, coding the ServerValidate event in
accordance with the ClientValidationFunction covers the possibility of
javascript being disabled on the client machine. I'm trying to
eliminate the call to the server for validation. The reason I'm using
BaseValidator is that it gives me ControlToCompare as well as
ControlToValidate. Inheriting from CustomControl doesn't give me
ControlToCompare. I'm trying to create a validating control that can
encompass all types of validation. What I'm still missing is the piece
that modifies the ErrorMessage property dynamically. For example, if
this field is required, I want the message to display 'Required' or if
it has an invalid format, display 'Invalid format', all of which is
done withouth going to the server. If these messages change, I'd like
to have a place where they're stored so I just change the actual
message and the control will pick it up without recompiling the
control.

I'm basing my control on what was discussed here:
http://aspalliance.com/371_Creating_A_Designer_Enabled_Custom_Validator_Control_Pt_II_Client_Side

Any feedback is appreciated.
 
B

Brennan Stehling

Technically this is what you want to do...

http://brennan.offwhite.net/blog/2004/12/15/replacing-your-inner-html/

However, your Javascript function to test your control value and
finding the block to place your message is another issue. If you are
using a ValidationSummary control you need to insert your new text in
place of what was there.

To my knowledge there is no Javascript API to adjust the ErrorMessage
on the client-side, but that would be quite useful. I know it can be
done, but without spending some time working out the details I could
not tell you what to do.

I think your attempt to create the big daddy of all validators may be a
very difficult chore. Look at how Peter Blum does it for some ideas.

http://www.peterblum.com/VAM/Home.aspx

Brennan Stehling
http://brennan.offwhite.net/blog/
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top