-2
我有代碼填充文本框,當我從ddl中選擇它們。 這工作很好,並在我的頁面加載方法。更新在Linq不提交更改()
protected void Page_Load(object sender, EventArgs e)
{
//drp_Customer.ClearSelection();
using (CustomerDataContext obj = new CustomerDataContext())
{
// Get fields for drp_Customer
// ===========================
var allCustomers = from c in obj.Customers
orderby c.FirstName
select new
{
c.FirstName,
c.CustomerId
};
drp_Customer.DataTextField = "FirstName";
drp_Customer.DataValueField = "CustomerId";
drp_Customer.DataSource = allCustomers.ToList();
drp_Customer.DataBind();
if (drp_Customer.SelectedItem.Text == " -- Select Customer -- ")
{
lbl_message.Text = "Please select a customer to update";
}
else
{
Customer myCust = obj.Customers.SingleOrDefault(c => c.CustomerId == Convert.ToInt32(drp_Customer.SelectedItem.Value));
if (myCust != null)
{
txt_FirstName.Text = myCust.FirstName;
txt_Surname.Text = myCust.Surname;
txt_HouseNumber.Text = myCust.HouseNumberName;
txt_Address.Text = myCust.Address;
txt_Town.Text = myCust.Town;
txt_Telephone.Text = myCust.Telephone;
txt_Postcode.Text = myCust.Postcode;
}
}
}
}
我有一個用於更新,但已停止工作,因爲此代碼的頁面上更新按鈕。
protected void btn_Update_Click(object sender, EventArgs e)
{
using (CustomerDataContext obj = new CustomerDataContext())
{
Customer myCust = obj.Customers.SingleOrDefault(c => c.CustomerId == Convert.ToInt32(drp_Customer.SelectedValue));
// Check If any are empty
// ======================
if (myCust != null)
{
myCust.FirstName = txt_FirstName.Text;
myCust.Surname = txt_Surname.Text;
myCust.HouseNumberName = txt_HouseNumber.Text;
myCust.Address = txt_Address.Text;
myCust.Town = txt_Town.Text;
myCust.Telephone = txt_Telephone.Text;
myCust.Postcode = txt_Postcode.Text;
}
obj.SubmitChanges();
}
}
我查詢相同CustomerID和當我通過代碼的 // myCust.FirstName = txt_FirstName.Text步驟; 當我修改它時不會改變。
頁面是否需要刷新才能執行更改? 或者我需要在頁面加載中以某種方式?