Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a web form that has two textboxes on it. I also have a RangeValidator on the web form for the textboxes.

Here is the properties for the RangeValidator:
ASP.NET
<asp:RangeValidator ID="RangeValidator1" runat="server" 
                    ControlToValidate="TextBox2" CssClass="style40" 
                    ErrorMessage="20% Difference. Please verify numbers" ForeColor="Red" 
                    Type="Integer" Enabled="True" Display="Dynamic"></asp:RangeValidator>


Textbox2 properties:
ASP.NET
<asp:TextBox ID="TextBox2" runat="server" Width="180px" AutoPostBack="False" 
                    ontextchanged="TextBox2_TextChanged">0</asp:TextBox>


Textbox1 is data from the database.
Textbox1 properties:
ASP.NET
<asp:TextBox ID="TextBoxLYFTUG" runat="server" Width="180px" 
                    ReadOnly="True" Enabled="False"></asp:TextBox>


Here is the code behind for the RangeValidator:
C#
double txtVal6 = Convert.ToDouble(TextBox1.Text);
        double minVal6 = (txtVal * 0.8);
        double maxVal6 = (txtVal * 1.2);
        RangeValidator1.MinimumValue = minVal6.ToString();
        RangeValidator1.MaximumValue = maxVal6.ToString();



As the code is right now it works fine. The RangeValidator fires the error message.

When apply the format code for textbox2 and setting the AutoPostBack to True, the RangeValidator does not fire but the data is formatted.

Here is the format code:
C#
TextBox1.Text = string.Format("{0:0,0}", double.Parse(TextBox1.Text));


Is there a way to keep the format and fire the validator on point? Or is there a work around that will work better?
Posted
Comments
Sergey Alexandrovich Kryukov 13-Oct-14 16:24pm    
Don't Convert.ToDouble. Use double.TryParse.
—SA
Sidtrey 23-Oct-14 11:13am    
Good point, but off subject.

1 solution

You could use AJAX to just update the specific control. I have used that method in several of my solutions.
 
Share this answer
 
Comments
Computer Wiz99 23-Oct-14 11:55am    
Sidtrey, I don't know how to use AJAX. I think I got it working so far.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900