如何在textchanged中以正确的格式获取输入字符串? [英] How to get input string in correct format at textchanged?

查看:102
本文介绍了如何在textchanged中以正确的格式获取输入字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我的表格上有文字框。每个文本框上都有一个TextChanged来进行一些计算。我有一个正则表达式验证器来检查用户是否输入了一个字符而不是一个数字。如果用户输入10525.5,则假设重新计算为10,526,但我收到此错误:



Hello to all.

I have a form that has textboxes on it. Each textbox has a TextChanged on it to do some calculations. I have a Regular Expression Validator in place to check on if the user is entering a character instead of a number. If a user enters 10525.5 it is suppose to recalculate to 10,526 but I get this error:

Input string was not in a correct format.





为什么我会收到此错误以及纠正错误的最佳方法是什么?



这是我的文本框的HTML代码:





Why am I getting this error and what is the best way to correct it?

Here is my HTML code for the textboxes:

<pre><asp:TextBox ID="TextBox1" runat="server" Width="180px" 

                    ToolTip="IPEDS Part C, Line 01, Column 01" AutoPostBack="True" 

                    ontextchanged="TextBox1_TextChanged" ValidationGroup="Check">0</asp:TextBox>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator14" 

                    runat="server" ControlToValidate="TextBox1" CssClass="style18" 

                    ErrorMessage="You Must Enter Instruction" ForeColor="Red" 

                    ValidationExpression="^[-]?\d+$." ValidationGroup="Check"></asp:RegularExpressionValidator>









这是用于计算和添加和删除的C#代码逗号:







Here is the C# code for the calculations and adding and removing commas:

protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(TextBox1.Text.Replace(",", ""));
            int b = Convert.ToInt32(TextBox2.Text.Replace(",", ""));
            int c = Convert.ToInt32(TextBox3.Text.Replace(",", ""));
            int d = Convert.ToInt32(TextBox4.Text.Replace(",", ""));
            int f = Convert.ToInt32(TextBox5.Text.Replace(",", ""));
            int g = Convert.ToInt32(TextBox6.Text.Replace(",", ""));
            
            int j = Convert.ToInt32(TextBox7.Text.Replace(",", ""));
            int k = Convert.ToInt32(TextBox8.Text.Replace(",", ""));
            int l = Convert.ToInt32(TextBox9.Text.Replace(",", ""));
            int m = Convert.ToInt32(TextBox10.Text.Replace(",", ""));
            TextBox88.Text = Convert.ToString(a + b + c + d + f + g + j);
            TextBox89.Text = Convert.ToString(a + b + c + d + f + g + j + k + l + m);

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





源错误位于:





The Source Error is at:

int a = Convert.ToInt32(TextBox1.Text.Replace(",", ""));





我的尝试:



我试图评论计算区域和舍入和数字转换的工作。





What I have tried:

I have tried to Comment out the calculation area and the rounding and number converted works.

protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            TextBox1.Text = string.Format("{0:0,0}", double.Parse(TextBox1.Text));
            TextBox2.Focus();
        }

推荐答案

< span class =code-attribute> ValidationGroup = 检查 > < / asp:RegularExpressionValidator >









以下是计算和添加和删除逗号的C#代码:







Here is the C# code for the calculations and adding and removing commas:

protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(TextBox1.Text.Replace(",", ""));
            int b = Convert.ToInt32(TextBox2.Text.Replace(",", ""));
            int c = Convert.ToInt32(TextBox3.Text.Replace(",", ""));
            int d = Convert.ToInt32(TextBox4.Text.Replace(",", ""));
            int f = Convert.ToInt32(TextBox5.Text.Replace(",", ""));
            int g = Convert.ToInt32(TextBox6.Text.Replace(",", ""));
            
            int j = Convert.ToInt32(TextBox7.Text.Replace(",", ""));
            int k = Convert.ToInt32(TextBox8.Text.Replace(",", ""));
            int l = Convert.ToInt32(TextBox9.Text.Replace(",", ""));
            int m = Convert.ToInt32(TextBox10.Text.Replace(",", ""));
            TextBox88.Text = Convert.ToString(a + b + c + d + f + g + j);
            TextBox89.Text = Convert.ToString(a + b + c + d + f + g + j + k + l + m);

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





源错误位于:





The Source Error is at:

int a = Convert.ToInt32(TextBox1.Text.Replace(",", ""));





我的尝试:



我试图评论计算区域和舍入和数字转换的工作。





What I have tried:

I have tried to Comment out the calculation area and the rounding and number converted works.

protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            TextBox1.Text = string.Format("{0:0,0}", double.Parse(TextBox1.Text));
            TextBox2.Focus();
        }


int a = Convert.ToInt32(TextBox1.Text.Replace(",", ""));





无论TextBox1.Text.Replace(,,)返回什么都无法转换为int。记住你的验证器就是这样,他们验证。您可以询问此控件是否有效?,它不会阻止用户在该框中输入无效字符,也不会从中删除无效字符。它只是让您在处理控件之前检查控件是否有效。所以在你的更改事件中使用验证器来查看控件是否有效以及是否显示错误而不是运行代码。



也使用int.TryParse而不是Convert.ToInt32。



Whatever TextBox1.Text.Replace(",", "") returns can't be converted to an int. Remember your validators are just that, they validate. You can ask "is this control valid?", it doesn't stop a user from entering invalid characters into that box and it doesn't remove invalid characters from it either. It simply lets you check if the controls are valid before you process them. So in your change event use the validator to see if the controls are valid and if not show an error rather than running your code.

Also use int.TryParse rather than Convert.ToInt32.


我有解决方案!!!我开始工作!!!



I have the solution!!! I got it to work!!!

protected void TextBoxInstr_TextChanged(object sender, EventArgs e)
        {

            int a = Convert.ToInt32(TextBox1.Text.Replace(",", "").Replace(".", ""));
            int b = Convert.ToInt32(TextBox2.Text.Replace(",", "").Replace(".", ""));
            int c = Convert.ToInt32(TextBox3.Text.Replace(",", "").Replace(".", ""));
            int d = Convert.ToInt32(TextBox4.Text.Replace(",", "").Replace(".", ""));
            int f = Convert.ToInt32(TextBox5.Text.Replace(",", "").Replace(".", ""));
            int g = Convert.ToInt32(TextBox6.Text.Replace(",", "").Replace(".", ""));
            
            int j = Convert.ToInt32(TextBox7.Text.Replace(",", "").Replace(".", ""));
            int k = Convert.ToInt32(TextBox8.Text.Replace(",", "").Replace(".", ""));
            int l = Convert.ToInt32(TextBox9.Text.Replace(",", "").Replace(".", ""));
            int m = Convert.ToInt32(TextBox10.Text.Replace(",", "").Replace(".", ""));
            TextBox88.Text = Convert.ToString(a + b + c + d + f + g + j);
            TextBox89.Text = Convert.ToString(a + b + c + d + f + g + j + k + l + m);


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





我添加了



I added

.Replace(".", "")

进行所有计算并且有效。

to all of the calculations and it worked.


这篇关于如何在textchanged中以正确的格式获取输入字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆