[英]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();
}
您可以使用以下之一:
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.