首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

3行的小疑点~

2012-01-16 
3行的小问题~~~~~~~~~~~~protectedvoidButton1_Click(objectsender,EventArgse){Label1.TextTextBox1.Tex

3行的小问题~~~~~~~~~~~~
protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
                Label1.Text   =TextBox1.Text+TextBox2.Text;
        }

这样运行的结果会是:输入5+8   ,运算却是58.
怎样得到5+8=13那?



[解决办法]
INT X =INT.PARSE(TextBox1.Text);
INT Y =INT.PARSE(TextBox2.Text);
int total =x+y;
Label1.Text =total.ToString();

[解决办法]
可以使用TryParse:

protected void Button1_Click(object sender, EventArgs e)
{
int i1 = 0;
int i2 = 0;

if (int.TryParse(TextBox1.Text, out i1) && int.TryParse(TextBox2.Text, out i2))
{
Label1.Text = (i1 + i2).ToString();
}
else
{
Label1.Text = "计算数无效! ";
}
}
[解决办法]
Label1.Text =TextBox1.Text+TextBox2.Text;

========

因为 Text 返回的的是 System.String , + 表示执行字符串连接

热点排行