C# 如何使用label.text写入结果

C# 如何使用label.text写入结果,c#,asp.net,C#,Asp.net,我尝试添加两个数字,然后显示结果 但不包括响应。写入(结果);因为我不能把它放在我想要的地方 我的aspx中有这个: <asp:TextBox label="tal1" ID="TextBox_Tal1" runat="server"></asp:TextBox> <asp:TextBox label="tal2" ID="TextBox_Tal2" runat="server"></asp:TextBox> <asp:Button ID

我尝试添加两个数字,然后显示结果

  • 但不包括响应。写入(结果);因为我不能把它放在我想要的地方
我的aspx中有这个:

<asp:TextBox label="tal1" ID="TextBox_Tal1" runat="server"></asp:TextBox>
<asp:TextBox label="tal2" ID="TextBox_Tal2" runat="server"></asp:TextBox>
<asp:Button ID="Button_plus" runat="server" Text="+" OnClick="Button_plus_Click" />
<asp:Label ID="Label_plus" runat="server" Text=""></asp:Label>

当前您正在调用
plus
,但忽略结果。我猜你想要的是:

Label_plus.Text = plus(tal1, tal2).ToString();
设置标签的内容,然后在响应中呈现该内容


不确定为
+
设置一个方法是否有意义,或者它是否应该是公共的,或者它是否应该被称为
plus
,以无视.NET命名约定,但这是一个稍微独立的问题。

目前您正在调用
plus
,但忽略了结果。我猜你想要的是:

Label_plus.Text = plus(tal1, tal2).ToString();
protected void Button_plus_Click(object sender, EventArgs e)
{
int tal1 = Convert.ToInt32(TextBox_Tal1.Text);
int tal2 = Convert.ToInt32(TextBox_Tal2.Text);
Label_plus.Text = (tal1 + tal2).ToString();        
}
设置标签的内容,然后在响应中呈现该内容

不确定为
+
设置一个方法是否有意义,或者它是否应该是公共的,或者它是否应该被称为
plus
,以无视.NET命名约定,但这是一个稍微独立的问题

protected void Button_plus_Click(object sender, EventArgs e)
{
int tal1 = Convert.ToInt32(TextBox_Tal1.Text);
int tal2 = Convert.ToInt32(TextBox_Tal2.Text);
Label_plus.Text = (tal1 + tal2).ToString();        
}
就可以了,不需要编写单独的函数

或者按照@Sleiman Jneidi的建议

int number1,number2;
bool result1 = Int32.TryParse(TextBox_Tal1.Text, out number1);
bool result2 = Int32.TryParse(TextBox_Tal2.Text, out number2);
if(result1 && result2){
// assign the result to the Text property
Label_result.Text = plus(number1,number2).ToString();
}
就可以了,不需要编写单独的函数

或者按照@Sleiman Jneidi的建议

int number1,number2;
bool result1 = Int32.TryParse(TextBox_Tal1.Text, out number1);
bool result2 = Int32.TryParse(TextBox_Tal2.Text, out number2);
if(result1 && result2){
// assign the result to the Text property
Label_result.Text = plus(number1,number2).ToString();
}

非常简单,只需将结果分配给
Text
属性即可。然而,您不应该信任用户的输入,您应该使用
TryParse

  int number1,number2;
  bool result1 = Int32.TryParse(TextBox_Tal1.Text, out number1);
  bool result2 = Int32.TryParse(TextBox_Tal2.Text, out number2);
  if(result1 && result2){
    // assign the result to the Text property
    Label_result.Text = plus(number1,number2).ToString(); 
  }

非常简单,只需将结果分配给
Text
属性即可。然而,您不应该信任用户的输入,您应该使用
TryParse

  int number1,number2;
  bool result1 = Int32.TryParse(TextBox_Tal1.Text, out number1);
  bool result2 = Int32.TryParse(TextBox_Tal2.Text, out number2);
  if(result1 && result2){
    // assign the result to the Text property
    Label_result.Text = plus(number1,number2).ToString(); 
  }

注意:您的方法
plus
返回一个值,但在调用它时您没有使用它
label.text=result.ToString()
您真的需要一个函数将两个数字相加吗?注意:您的方法
plus
返回一个值,但在调用它时您没有使用它
label.text=result.ToString()
您真的需要一个函数将两个数字相加吗?