C# 在标签中显示整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10890962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:40:02  来源:igfitidea点击:

Displaying Integer in a Label

c#asp.net

提问by unknownsatan

How can I display an integer in a Label? What I am doing is I am calculating the total and I am trying to display it in a label.

如何在标签中显示整数?我正在做的是计算总数,并尝试将其显示在标签中。

public partial class total : System.Web.UI.Page
{
    int total;
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Server.HtmlEncode(Request.Cookies["confirm"]["quantity"]);
        int quantity = (int)Session["TextBox1Value"];

        if (Request.Cookies["user"]["items"] == "Tyres")
        {
            total = 20 * quantity;

            Label2.Text = ???
        }
    }
}

Or is there any other way to display the total on same page?

或者有没有其他方法可以在同一页面上显示总数?

采纳答案by Nikhil Agrawal

Use

Label2.Text = total.ToString();

OR

或者

Label2.Text =  Convert.ToString(total);

Since Texttakes a string so you have to convert your totalinteger value to string by calling ToString()or Convert.ToString(int).

由于Text需要一个字符串,因此您必须total通过调用ToString()或将整数值转换为字符串Convert.ToString(int)

回答by stay_hungry

You can do it using .ToString() or Convert.ToString() methods.

您可以使用 .ToString() 或 Convert.ToString() 方法来完成。

Label2.Text = total.ToString();

or

或者

Label2.Text =  Convert.ToString(total);