【发布时间】:2020-02-21 08:36:35
【问题描述】:
我在 ASP .NET 中有一个页面,当我访问它时,我以编程方式在 TextBox 中设置了一个值。当我单击按钮时,我想更新该值,但它给了我错误:
对象未定义
这是我的代码:
public partial class InsertValues : System.Web.UI.Page
{
DataProvider dataProvider = new DataProvider(); // This class contains all the querys I need to pull data
public MyValuesClass myValues; // This is my class where I get the data from my DB
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
startMyPage(); // Function that gets the values from the DataBase and sets my TextBox with the values.
}
else
{
}
}
private void startMyPage()
{
myValues = dataProvider.getValuesFromDB(); // Function that gets the values from a query and put them in my class, the values are. String "Banana" and Bool isNew = True
if (!myValues.isNew) //
{
txtFood.Text = myValues.food
}
else
{
myValues= new myValues();
myValues.isNew = true;
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (myValues.isNew) // Object not defined.
{
dataProvider.addMyValues(myValues); // It Inserts into my DB
}
else
{
dataProvider.editMyValues(myValues); // It Updates into my DB
}
}
}
基本上,在我单击“btnSave”后,类 myValues 变为 null,并且我收到错误 Object not defined,有没有办法维护该类价值观?
【问题讨论】:
标签: asp.net webforms buttonclick