這裏的代碼,我可能會犯回發錯誤,對我來說不是很清楚。在任何情況下,我已經在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();
}
}
請分享一些代碼,以供參考此問題。 –
沒有看到你的代碼,我猜你在覆蓋PostBack中的數據。使用'if(!IsPostBack){//綁定所有數據}' – VDWWD
對不起,我嘗試添加一些代碼,但是我的評論太長了。我試着去了解我該怎麼做。 –