asp.netvalidationsummaryrangevalidator

Why does ErrorMessage not render correctly


Using ASP-TextBox within html-table(td) within FormView EditItemTemplate, within ASP-Panel, within AJAX panel, within Content-page and within Master-Page, the ValidationSummary shows the wrong ErrorMessage when validating the textbox with the SAVE-button.

The ValSummary shows "Mileage entered is less than '<%= txtMileageRangeValidator.MinimumValue %>' miles".

And NOT "Mileage entered is less than 88123 miles"

The markup for the textbox and validator follows:

<asp:TextBox ID="txtMileage" runat="server" Text='<%# Bind("Mileage") %>' 
   CssClass="ucIsRequired"
   MaxLength="6" 
   AutoPostBack="True" OnTextChanged="txtMileage_TextChanged" 
/>
<asp:RangeValidator ID="txtMileageRangeValidator" runat="server" 
   ControlToValidate="txtMileage"
   Enabled="true"
   Display="None"
   MinimumValue='<%# Eval("aMileagePrev", "{0:D}")%>' 
   MaximumValue="999999"
   SetFocusOnError="false" 
   Type="Integer"
   ValidationGroup="valgrpDetails"
   ErrorMessage="Mileage entered is less than '<%= txtMileageRangeValidator.MinimumValue %>' miles"
   />

However, when the error appears in the ValidationSummary, it appears exactly as listed above -- it does not substitute the MinimumValue into the error-text.

What am I doing wrong? Thanks.


Solution

  • Unless disabled, RangeValidators validate on the client side, i.e. no postback, so server side embedded script will not be re-rendered

    If your going to reset the min value on the textbox event then you can set the ErrororMessage as you do for the MinimumValue but with a modified string format

    ErrorMessage='<%# Eval("aMileagePrev", "Mileage entered is less than {0:D} miles") %>'
    

    But something else now that I'm reading closer. A range validation is intended to check for a value being between min/max values. You're making an assumption that the value will never be greater than maxValue. If this is always the case then you would be better served with a CompareValidator with Type set to Integer and Operator set to LessThan