繁体   English   中英

如何在文本字段中显示int值?

[英]How to display int values in a textfield?

我正在Visual Studio中开发Web表单应用程序,并且正在尝试构建更新网格。

我可以从记录中引入string值,但是要像in Age这样引入int值时遇到麻烦。

我已经在下面发布了我的代码。

码:

private void DisplayPersonData(Author p)
{
    txtFName.Text = p.Name;
    txtAge.Text = p.Age;//Problem is here 
}

protected void btnSearchId_Click(object sender, EventArgs e)
{
    int id = System.Convert.ToInt32(txtId.Text);
    hfId.Value = id.ToString();
    targetPerson = GetPersonById(id);
    DisplayPersonData(targetPerson);
}

protected void btnUpdate_Click(object sender, EventArgs e)
{
    targetPerson = GetPersonById(Convert.ToInt32(hfId.Value));
    targetPerson.Name = txtFName.Text;
    targetPerson.Age = txtAge.Text;//Problem is here 

    context.SaveChanges();
} 

我想我需要将int转换为string ,但是我不确定该怎么做?

只需在保存时将其转换为int并在设置该值时将其转换回字符串,

 protected void btnUpdate_Click(object sender, EventArgs e)
    {
        targetPerson = GetPersonById(Convert.ToInt32(hfId.Value));
        targetPerson.Name = txtFName.Text;
        targetPerson.Age =  Convert.ToInt32(txtAge.Text);
        context.SaveChanges();
    } 

private void DisplayPersonData(Author p)
    {
        txtFName.Text = p.Name;
        txtAge.Text = p.Age.ToString(); 
    }

method to convert the age integer value to string as shown below: 您可以使用方法将年龄整数值转换为字符串,如下所示:

txtAge.Text = p.Age.ToString();

或者您可以执行以下操作:

txtAge.Text = Convert.ToString(p.Age);

此外,如果需要进一步将其用于计算,则必须将其转换回Integer,并且可以按照以下步骤操作:

Int32 Age = Convert.ToInt32(txtAge.Text);

有关更多详细信息,您可以访问此处此处

您可以使用以下之一:

txtAge.Text = Convert.ToString(p.Age);
targetPerson.Age = Convert.ToString(txtAge.Text);

要么

txtAge.Text = "" + p.Age;
targetPerson.Age = ""+ txtAge.Text;

要么

txtAge.Text = p.Age.ToString();
targetPerson.Age = txtAge.Text.ToString();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM