0

我已经通过我的代码查看是否存在任何空值,但每个人都带来了价值,但我找不到问题。

public partial class UserDetail : ICSBaseUserControl
{
    UserRepository userDao = new UserRepository();
    User user;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            long userID = Convert.ToInt64(Request[UrlParameters.UrlParameterName.UserID]);
            user = (userID > 0) ? userDao.GetUser(AppSession.Company.ID, userID) : new c365_EntityFramework.User();
            if (user.ID > 0)
            {
                txtFirstName.Text = user.Forename;
                txtSurname.Text = user.Surname;
                txtAddress.Text = user.Address1;
                txtEmail.Text = user.Email;
                txtUsername.Text = user.Username;

            }
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        bool result = userDao.UpdateUser(user.ID, txtFirstName.Text, txtEmail.Text, txtSurname.Text, txtAddress.Text, txtUsername.Text);
    }
}

编辑

这就是我修复它的方法,将user加载移出if(!IsPostBack)

public partial class UserDetail : ICSBaseUserControl
{
    UserRepository userDao = new UserRepository();
    User user;
    protected void Page_Load(object sender, EventArgs e)
    {
        long userID = Convert.ToInt64(Request[UrlParameters.UrlParameterName.UserID]);
        user = (userID > 0) ? userDao.GetUser(AppSession.Company.ID, userID) : new c365_EntityFramework.User();
        if (!IsPostBack)
        {
            if (user.ID > 0) // Check for user ID
            {
                txtFirstName.Text = user.Forename;
                txtSurname.Text = user.Surname;
                txtAddress.Text = user.Address1;
                txtEmail.Text = user.Email;
                txtUsername.Text = user.Username;
            }
        }
    }
4

1 回答 1

4

我认为问题在于该字段user没有被实例化,因此将null在您的按钮单击上。

如果不是回发,您Page_Load唯一的填充。那么,当您按下按钮时,回发正在进行中,而您当时没有收到?此外,您需要查看 ASP .NET 页面生命周期。useruser

ASP .NET 页面记得字段,您可能希望将其user置于会话状态,然后以这种方式使用它。

于 2013-08-22T11:30:21.610 回答