Change ErrorMessage Text Dynamically

Discussion in 'ASP .Net Building Controls' started by jdp, Oct 3, 2006.

  1. jdp

    jdp Guest

    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, Oct 3, 2006
    #1
    1. Advertising

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

    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.
    Brennan Stehling, Oct 3, 2006
    #2
    1. Advertising

  3. jdp

    jdp Guest

    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.

    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/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/
    jdp, Oct 4, 2006
    #3
  4. 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/

    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_Client_Side
    >
    > 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/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/
    Brennan Stehling, Oct 4, 2006
    #4
  5. jdp

    jdp Guest

    Thanks, I will.

    Brennan Stehling wrote:
    > 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/
    >
    jdp, Oct 4, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PeterKellner
    Replies:
    2
    Views:
    470
    PeterKellner
    May 17, 2006
  2. jdp
    Replies:
    0
    Views:
    294
  3. Andy

    Validator Text vs. ErrorMessage

    Andy, Jul 14, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    676
  4. Andrew Backer

    Validation Controls : .ErrorMessage & .Text relationship

    Andrew Backer, Mar 8, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    109
    MasterGaurav
    Mar 9, 2005
  5. jdp

    Change ErrorMessage Text Dynamically

    jdp, Oct 3, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    106
Loading...

Share This Page