Click here to Skip to main content
15,907,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a web form that has four textboxes on it. I also have three RangeValidators on the three of the textboxes. When the user enters numbers in textbox1 and textbox2 the RangeValidator fires for both textboxes and the textbox does not format until the user tabs to textbox3. Also the first I am just trying to find out why? What did I do wrong?

Textbox1 has this code behind and properties:
ASP.NET
<asp:TextBox ID="TextBox1" runat="server" Width="180px" AutoPostBack="True" CausesValidation="True">0</asp:TextBox>

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


Textbox2 has this code behind and properties:
ASP.NET
<asp:TextBox ID="TextBox2" runat="server" Width="180px" AutoPostBack="True" CausesValidation="True">0</asp:TextBox>

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


Textbox3 has this code behind and properties:
ASP.NET
<asp:TextBox ID="TextBox3" runat="server" Width="180px" AutoPostBack="True" ontextchanged="TextBox3_TextChanged">0</asp:TextBox>

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

C#
protected void TextBoxTHUG_TextChanged(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(TextBox3.Text.Replace(",", ""));
        int j = 12;
        TextBox4.Text = Convert.ToString(i / j);
        RangeValidatorLY4.Validate();
        TextBox5.Focus();
    }



Textbox4 has this code behind and properties:
ASP.NET
<asp:TextBox ID="TextBox4" runat="server" Width="180px" Enabled="False" ReadOnly="True" CausesValidation="True" AutoPostBack="True"></asp:TextBox>
Posted
Comments
Sinisa Hajnal 15-Oct-14 9:28am    
In which event are you formatting txt 1 and 2? It seems your code behind doesn't get fired until TextBox2_TextChanged...
Computer Wiz99 15-Oct-14 9:29am    
The format is in the Page load event.
Sinisa Hajnal 15-Oct-14 9:44am    
And why would they format except when server postback is done? You should have their own text changed, OnValidating (or Validated) handlers after your validation triggers they should format.

Initial value SHOULD be formatted by the code you provided. Please ensure that their values are really set in Page_Load (That they are not hidden behind !Postback or something)
Computer Wiz99 15-Oct-14 9:50am    
Here is my Page Load code:

protected void Page_Load(object sender, EventArgs e)
{

this.TextBoxDATE.Text = DateTime.Today.ToString("dd-MMM-yy");
this.lblTime.Text = System.DateTime.Now.ToShortTimeString();


TextBox1.Text = string.Format("{0:0,0}", double.Parse(TextBox1.Text));
TextBox2.Text = string.Format("{0:0,0}", double.Parse(TextBox2.Text));
TextBox3.Text = string.Format("{0:0,0}", double.Parse(TextBox3.Text));
TextBox4.Text = string.Format("{0:0,0}", double.Parse(TextBox4.Text));
}

Sinisa Hajnal 15-Oct-14 9:56am    
OK, superb. And what is the initial value of your TextBox1.Text? Empty string? And then when you enter the data it gets formatted...but only once you get into PAGE_LOAD again which happens when your TextBox3 triggers its TextChanged.

String.Format does not enable the textbox to format whatever is entered. It is a single command that formats the text when it is executed.

1 solution

If you want to have textboxes which format the text entered or prevent users from entering just anything, use MaskedTextBox from AjaxToolkit or use javascript to format the text client side.

Otherwise, create formatting function that takes TextBox control and formats its text. You can use it in TextChanged event, but you have to create event handler for each textbox.

In your page
ASP.NET
<asp:textbox id="TextBox1" runat="server" width="180px" autopostback="True" xmlns:asp="#unknown">
ontextchanged="TextBoxChanged_Handler">0</asp:textbox>

<asp:textbox id="TextBox2" runat="server" width="180px" autopostback="True" ontextchanged="TextBoxChanged_Handler" xmlns:asp="#unknown">0</asp:textbox>



Code behind:
C#
protected TextBoxChanged_Handler(object sender, EventArgs e) {
FormatText(sender);
}

// you could add some type checking before sending the sender too
private void FormatText(TextBox txt) {
txt.Text = string.Format("{0:0,0}", double.Parse(txt));
}



You should rename your controls to something sensible.

If this helps, please take time to accept the solution. Thank you.
 
Share this answer
 
v2

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