[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.buildingcontrols

Change ErrorMessage Text Dynamically

jdp

10/3/2006 5:13:00 PM

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.

4 Answers

offwhite

10/3/2006 9:18:00 PM

0

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/library/system.web.ui.webcontrols.customvalidator.serverval...

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

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfun...

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/default.asp?url=/library/en-us/cpguide/html/cpconResourcesInResxFile...

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...

jdp wrote:
> 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.

jdp

10/4/2006 2:32:00 PM

0

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_C...

Any feedback is appreciated.

Brennan Stehling wrote:
> 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/library/system.web.ui.webcontrols.customvalidator.serverval...
>
> Then you can set the ClientValidationFunction which points to the
> Javascript function which runs on the client-side.
>
> http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfun...
>
> 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/default.asp?url=/library/en-us/cpguide/html/cpconResourcesInResxFile...
>
> 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...

offwhite

10/4/2006 3:14:00 PM

0

Technically this is what you want to do...

http://brennan.offwhite...2004/12/15/replacing-your-i...

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...

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

jdp wrote:
> 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_C...
>
> Any feedback is appreciated.
>
> Brennan Stehling wrote:
> > 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/library/system.web.ui.webcontrols.customvalidator.serverval...
> >
> > Then you can set the ClientValidationFunction which points to the
> > Javascript function which runs on the client-side.
> >
> > http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfun...
> >
> > 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/default.asp?url=/library/en-us/cpguide/html/cpconResourcesInResxFile...
> >
> > 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...

jdp

10/4/2006 3:50:00 PM

0

Thanks, I will.

Brennan Stehling wrote:
> Technically this is what you want to do...
>
> http://brennan.offwhite...2004/12/15/replacing-your-i...
>
> 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...
>
> Brennan Stehling
> http://brennan.offwhite...
>