Change ErrorMessage Text Dynamically

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Change ErrorMessage Text Dynamically

    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 Guest

  2. Similar Questions and Discussions

    1. Anyway to Dynamically Change the Color of Text
      Hello Forum Folks, I'm attempting to change the color of some text depending on the value of it's content, however when I try this, I get an...
    2. Change Content Dynamically
      I'm trying to find a way to change the contents of a table (calendar) that I have nested in a DIV. without loading a new page. In other words, go...
    3. Change DataNavigateUrlField dynamically
      Hi guys, let's see if someone can give me a small hand with this.... i'm populating a dataset that sometimes changes 1 or 2 columns depending on...
    4. Dynamically change URL in Web Service
      Dear Al I am now also facing a problem on changing URL in web reference. Firstly, i added a web reference in my project, and then i change the...
    5. dynamically change
      I need to know how to modify the <asp:BoundColumn/> to N/A when the datafield value comes in as negative quantity and color it different on the...
  3. #2

    Default Re: Change ErrorMessage Text Dynamically

    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.

    [url]http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.serverva lidate.aspx[/url]

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

    [url]http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientva lidationfunction.aspx[/url]

    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.

    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconResourcesInResxFileFormat.asp[/url]

    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
    [url]http://brennan.offwhite.net/blog/[/url]

    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 Guest

  4. #3

    Default Re: Change ErrorMessage Text Dynamically

    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:
    [url]http://aspalliance.com/371_Creating_A_Designer_Enabled_Custom_Validator_C ontrol_Pt_II_Client_Side[/url]

    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.
    >
    > [url]http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.serverva lidate.aspx[/url]
    >
    > Then you can set the ClientValidationFunction which points to the
    > Javascript function which runs on the client-side.
    >
    > [url]http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientva lidationfunction.aspx[/url]
    >
    > 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.
    >
    > [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconResourcesInResxFileFormat.asp[/url]
    >
    > 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
    > [url]http://brennan.offwhite.net/blog/[/url]
    jdp Guest

  5. #4

    Default Re: Change ErrorMessage Text Dynamically

    Technically this is what you want to do...

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

    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.

    [url]http://www.peterblum.com/VAM/Home.aspx[/url]

    Brennan Stehling
    [url]http://brennan.offwhite.net/blog/[/url]

    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:
    > [url]http://aspalliance.com/371_Creating_A_Designer_Enabled_Custom_Validator_C ontrol_Pt_II_Client_Side[/url]
    >
    > 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.
    > >
    > > [url]http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.serverva lidate.aspx[/url]
    > >
    > > Then you can set the ClientValidationFunction which points to the
    > > Javascript function which runs on the client-side.
    > >
    > > [url]http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientva lidationfunction.aspx[/url]
    > >
    > > 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.
    > >
    > > [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconResourcesInResxFileFormat.asp[/url]
    > >
    > > 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
    > > [url]http://brennan.offwhite.net/blog/[/url]
    Brennan Stehling Guest

  6. #5

    Default Re: Change ErrorMessage Text Dynamically

    Thanks, I will.

    Brennan Stehling wrote:
    > Technically this is what you want to do...
    >
    > [url]http://brennan.offwhite.net/blog/2004/12/15/replacing-your-inner-html/[/url]
    >
    > 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.
    >
    > [url]http://www.peterblum.com/VAM/Home.aspx[/url]
    >
    > Brennan Stehling
    > [url]http://brennan.offwhite.net/blog/[/url]
    >
    jdp Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139