2017-07-17 39 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(); 
     } 

    }