Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a web form that has eight textboxes and eight rangevalidators on it. When a user enters their data the numbers are formatted on textbox text change with commas. Here is the format code I am using:

C#
TextBox1.Text = string.Format("{0:0,0}", double.Parse(TextBox1.Text));


The format works fine until the user clicks on submit and four out of eight validators fire. What could have caused the firing of the validators and how can I fix it?

C#
protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        RangeValidator1.Validate();
        TextBox1.Text = string.Format("{0:0,0}", Int32.Parse(TextBox1.Text));
        TextBox2.Focus();
    }
Posted
Updated 28-Oct-14 4:29am
v2
Comments
Kornfeld Eliyahu Peter 28-Oct-14 9:58am    
It is probably the Text = [anything] that triggers TextChange and validation, not the format!
Computer Wiz99 28-Oct-14 10:07am    
I took the format out and the submission worked. Is there any way to rewrite the format code and still get the format?
Kornfeld Eliyahu Peter 28-Oct-14 10:11am    
You didn't understood me...
You are taking the value of the text-box, format it for display, than push back to the text-box. The formatted value no longer validates so your submission fails...
You probably have to look for some masked text-box control...
Computer Wiz99 28-Oct-14 10:17am    
I got what you are saying. I also have this code to remove the commas during the validation: Convert.ToInt32(TextBox1.Text.Replace(",", "")); Then the format is placed back in the numbers.
Kornfeld Eliyahu Peter 28-Oct-14 10:27am    
There is a chance that the line above is part of the TextChange event handler?

1 solution

Tente usar o mesmo CultureInfo tanto no validador quanto no String.Format

Ex.:

C#
Convert.ToDecimal(TextBox.Text).ToString(CultureInfo.GetCultureInfo("en-US"))
 
Share this answer
 
Comments
Computer Wiz99 28-Oct-14 10:36am    
Bruno Santos Oliverira, I am not using a Decimal. I don't need a decimal.
Bruno S Oliveira 28-Oct-14 12:10pm    
Convert Double to and after use the ToString:

TextBox1.Text = Convert.ToDouble(TextBox1.Text).ToString(CultureInfo.GetCultureInfo("en-US"))

ToString returns you a String!!

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