当单击gridview 中的链接按钮时,我试图填充Person 对象,然后获取其关联数据并显示在模式内的文本框中。我能够获取数据并将其传递给 Person 对象,但是,当我填充我的模态控件时,Person 对象在所有属性上都返回 null。
知道如何在不调用 Person objPerson = new Person() 的情况下将 Person 对象的值传递给文本框控件吗?
先感谢您。
实体:
public class Person
{
public int PersonID { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
数据层:
public DataTable SelectQueryProc(String _storedProc, SqlParameter[] sqlParameter)
{
DataTable t = new DataTable();
try
{
using (SqlConnection sqlConnection = new SqlConnection(CommonEntity.ConnectionString))
{
sqlConnection.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter(_storedProc, sqlConnection))
{
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.SelectCommand.Parameters.AddRange(sqlParameter);
adapter.Fill(t);
}
}
}
catch (SqlException e)
{
throw new SystemException(e.ToString());
}
finally
{
}
return t;
}
public List<Person>GetPersonSingleByPersonID(string personID)
{
List<Person> objPerson = new List<Person>();
DataTable dt = new DataTable();
SqlParameter[] sqlParam = new SqlParameter[1];
sqlParam[0] = new SqlParameter("@PersonID", SqlDbType.VarChar);
sqlParam[0].Value = personID;
CommonDAL commonDAL = new CommonDAL();
dt = commonDAL.SelectQueryProc("GetPersonSingleByPersonID", sqlParam);
foreach (DataRow dr in dt.Rows)
{
objPerson.Add(new Person()
{
PersonID = dr["PersonID"].ToString(),
Firstname = dr["Firstname"].ToString(),
Lastname = dr["Lastname"].ToString()
});
}
return objPerson;
}
业务逻辑层:
public List<Person> GetPersonSingleByPersonID(string personID)
{
PersonDAL objPersonDAL = new PersonDAL();
return objPersonDAL.GetPersonSingleByPersonID(companyID);
}
表示层:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "click":
{
// Populate Person Details
PersonBL objPersonBL = new PersonBL();
objPersonBL.GetPersonSingleByPersonID(e.CommandArgument.ToString());
Person objPerson = new Person();
txtPersonID.Text = objPerson.PersonID;
txtFirstname.Text = objPerson.Firstname;
txtLastname.Text = objPerson.Lastname;
break;
}
default:
break;
}
}