2017-07-17 97 views
-1

我是asp.net的新手,我有一个问题。我加载更新一个aspx页面,使用查询字符串,如edit.aspx?Id=123。在Load事件中,我读取查询字符串并加载正确的记录。通过一个按钮,我会更新记录,所以我将变量分配给文本框的新值,但我不明白为什么不考虑新文本框的值。我以这种方式分配值(例如new_email = txemail.text),但系统在new_email变量中分配相同的编辑记录值。 有人可以帮我理解我做错了什么吗? 在此先感谢。textbox的值已更改,但aspx页面不更新

+1

请分享一些代码,以供参考此问题。 –

+0

没有看到你的代码,我猜你在覆盖PostBack中的数据。使用'if(!IsPostBack){//绑定所有数据}' – VDWWD

+0

对不起,我尝试添加一些代码,但是我的评论太长了。我试着去了解我该怎么做。 –

回答

0

这里的代码,我可能会犯回发错误,对我来说不是很清楚。在任何情况下,我已经在updateRecord void中尝试过(!IsPostBack)或(IsPostBack),但在任何情况下,变量都不会获取更新的值。

 protected void Page_Load(object sender, EventArgs e) 
    { 
     // check if querystring has value 
     if (Request.QueryString["Id"] != null) 
     { 
      int idUser = Convert.ToInt32(Request.QueryString["Id"]); 
      loadRecord(idUser); 
     } 
    } 



protected void btSave_Click(object sender, EventArgs e) 
     { 
      // update record, deliberately simply, just for test purposes 
      int idUser = Convert.ToInt32(Request.QueryString["Id"]); 
      updateRecord(idUser); 

     } 

    private void updateRecord (int idRecord) 
    { 
     string new_FirstName = string.Empty; 
     string new_LastName = string.Empty; 
     string new_Email = string.Empty; 

     try 
     { 
      if(IsPostBack) 
      { 
       new_FirstName = txFirstName.Text; 
       new_LastName = txLastName.Text; 
       new_Email = txEmailAddr.Text; 
      } 


      using (AddressBookEntities abe = new AddressBookEntities()) 
      { 
       User updateUser = (from user in abe.Users 
          where user.IdUser == idRecord 
          select user).FirstOrDefault(); 

       updateUser.FirstName = new_FirstName; 
       updateUser.LastName = new_LastName; 
       updateUser.Email = new_Email; 

       abe.SaveChanges();      

      } 

     } 
     catch (Exception ex) 
     { 
      txError.Text = ex.ToString(); 
     } 

    }