// id 参数名应该与控件上设置的 DataKeyNames 值匹配 public void lvDepartmentMonitor_UpdateItem(Int64 id) { TextBox txtEditValue = new TextBox(); txtEditValue = (TextBox)lvDepartmentMonitor.EditItem.FindControl("txtValue"); var value = txtEditValue?.Text; if (String.IsNullOrEmpty(value)) { return; } using (MonitorDbContext context = new MonitorDbContext()) { IMS.Models.DepartmentMonitor item = null; // 在此加载该项,例如 item = MyDataLayer.Find(id); item = context.DepartmentMonitors.Find(id); if (item == null) { // 未找到该项 ModelState.AddModelError("", String.Format("未找到 id 为 {0} 的项", id)); return; } TryUpdateModel(item); if (ModelState.IsValid) { // 在此保存更改,例如 MyDataLayer.SaveChanges(); item.Value = value; //database win bool saveFailed; do { saveFailed = false; try { context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { saveFailed = true; // Update the values of the entity that failed to save from the store ex.Entries.Single().Reload(); } } while (saveFailed); } } }
// id 参数名应该与控件上设置的 DataKeyNames 值匹配 public void lvDepartmentMonitor_DeleteItem(Int64 id) { using (MonitorDbContext context = new MonitorDbContext()) { IMS.Models.DepartmentMonitor item = null; item = context.DepartmentMonitors.Find(id); if (item == null) { // 未找到该项 ModelState.AddModelError("", String.Format("未找到 id 为 {0} 的项", id)); return; } TryUpdateModel(item); if (ModelState.IsValid) { // 在此保存更改,例如 MyDataLayer.SaveChanges(); context.DepartmentMonitors.Remove(item); //database win bool saveFailed; do { saveFailed = false; try { context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { saveFailed = true; // Update the values of the entity that failed to save from the store ex.Entries.Single().Reload(); } } while (saveFailed); } } }