2013-08-22 134 views
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; 
      } 
     } 
    } 
+2

哪一行是错误? – Paddyd

+0

你在哪里得到这个错误? –

+0

bool result = userDao.UpdateUser(user.ID,txtFirstName.Text,txtEmail.Text,txtSurname.Text,txtAddress.Text,txtUsername.Text); – user2520671

回答

4

我相信问题是场user没有被实例化因此您的按钮点击将会是null

您的Page_Load仅填充user如果不是回发。所以,当你按下按钮回发正在进行中,并且你没有得到那个时候的user?另外,您需要查看ASP .NET页面生命周期。

ASP .NET页面做记得领域,你可能想把user到会话状态,然后用这种方式。

+0

不,它没有工作 – user2520671

+0

当你说不,它没有工作时,你有什么尝试/完成?将它存储在会话中?回顾一下?什么?在新的问题或编辑中显示您的更新代码。 – Belogix

+0

它没有正常工作 – user2520671

相关问题