如何在asp.net中计算格式化的文本框? [英] How to calculate a formatted textbox in asp.net?

查看:104
本文介绍了如何在asp.net中计算格式化的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含文本框的网络表单。我正在使用这种格式的文本框:



 TextBox1.Text =  string  .Format(  {0:0,0} .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));





我还有一个Textbox textchange来进行一些计算。当计算完成它的工作时,用户会收到此错误:



输入字符串的格式不正确。





以下是其中一个文本框文本更改的计算代码:



  protected   void  TextBox1_TextChanged(对象发​​件人,EventArgs e)
{
int i = Convert.ToInt32(TextBox1.Text);
int j = 12 ;
TextBox5.Text = Convert.ToString(i / j);
}





如何更正此错误才能进行计算?

解决方案

因为您正在将字符串值解析为整数值(或者在String.Format()块中解析)。您需要知道'a'不能转换为整数。而'0'可以。



我的意思是,该字符串包含一些不是有效数字的字符。您需要确保字符是数字。



Quote:

输入字符串的格式不正确。





上述语句告诉所有内容,您需要确保字符实际上是数字,并且可以分别转换为double或整数。



在MSDN上,一个快速的Google导致了这个链接, http://msdn.microsoft.com/en-us/library/bb397679.aspx [ ^ ]已明确将String转换为其他格式(如Integer,Double等)时,需要小心,因为String可以包含字母数字内容,而double,int,float仅限于数字。



唯一可以解决这个问题的方法是从文本框中删除非数字的所有字符,甚至是空格(使用 .Trim( )在字符串上删除开头或结尾的额外空格)。



由于您使用的是ASP.NET,因此在ASP.NET中有一个函数用于确定字符串是否为int,您可以使用



  if (variableName.IsInt()){
// 是,变量具有int值
// 解析它们,转换它们,使用它们
} else {
// 提示输入有效数据
}





您可以尝试许多其他方法,但请记住,总是使用 if else 阻止检查小像这样的条件。


I have a web form that has textboxes on it. I am using this format for the textboxes:

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));



I also have a Textbox textchange in place to do some calculations. When it is time for the calculations to do what it does the user gets this error:

Input string was not in a correct format.



Here is the calculation code for one of the textbox text change:

protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(TextBox1.Text);
        int j = 12;
        TextBox5.Text = Convert.ToString(i / j);
    }



How can I correct this error to do the calculations?

解决方案

Since you're parsing the string values to integer values (or double as in the String.Format() block). You need to know that 'a' cannot be converted to an integer. Whereas '0' can be.

What I mean is, that the string contains some characters that are not valid numbers. You need to make sure that the characters are numbers.

Quote:

Input string was not in a correct format.



Above statements tells it all, you need to make sure that the characters are actually numbers and can be converted to double or integers respectively.

On MSDN, a quick Google resulted in this link, http://msdn.microsoft.com/en-us/library/bb397679.aspx[^] where it has been made clear that while converting String to other formats like Integer, Double etc, you need to be carefull because String can contain alphanumeric content, whereas double, int, float are restricted to numbers only.

Only way to correct this, is to remove all of the characters from the textboxes that are not numbers, even whitespaces (use .Trim() on the string to remove extra spaces at start or end).

Since you're using ASP.NET, in ASP.NET there is a function used to determine whether the string is an int or not, you can use as

if(variableName.IsInt()) {
   // yes, the variable has int values
   // parse them, convert them, use them
} else {
   // prompt for a valid data
}



You can try out many other ways, but remember, always use if else block to check for small conditions like this one.


这篇关于如何在asp.net中计算格式化的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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