/// <summary> /// This function will confirm the last modification /// and do a batch save. /// </summary> protected void btnSaveAll_Click(object sender, EventArgs e) { //Default the value is false, meaning that the db isn't saved bool flag = false; DBProcess p = new DBProcess(); DataTable dt = ViewState["dt"] as DataTable; // Change states and do a batch update for (int i = 0; i < GridView1.Rows.Count; i++) { if ((GridView1.Rows[i].FindControl("chkDelete") as CheckBox).Checked) { dt.Rows[i].Delete(); flag = true; } else { if (dt.Rows[i]["PersonName"].ToString() != (GridView1.Rows[i].FindControl("tbName") as TextBox).Text) { if (dt.Rows[i].RowState == DataRowState.Unchanged) { dt.Rows[i].BeginEdit(); } dt.Rows[i]["PersonName"] = (GridView1.Rows[i].FindControl("tbName") as TextBox).Text; if (dt.Rows[i].RowState == DataRowState.Unchanged) { dt.Rows[i].EndEdit(); } flag = true; } if (dt.Rows[i]["PersonAddress"].ToString() != (GridView1.Rows[i].FindControl("tbAddress") as TextBox).Text) { if (dt.Rows[i].RowState == DataRowState.Unchanged) { dt.Rows[i].BeginEdit(); } dt.Rows[i]["PersonAddress"] = (GridView1.Rows[i].FindControl("tbAddress") as TextBox).Text; if (dt.Rows[i].RowState == DataRowState.Unchanged) { dt.Rows[i].EndEdit(); } flag = true; } } } p.BatchSave(dt); // Save data into db, so no need to maintain the state colors HidState.Value = "[]"; dt = p.GetDataTable(); GridView1.DataSource = dt; GridView1.DataBind(); if (flag) { ClientScript.RegisterStartupScript (GetType(), "js", "alert('Save All Changes successfully!');", true); } }
/// <summary> /// 此函数将会确认最近的修改并执行批保存. /// </summary> protected void btnSaveAll_Click(object sender, EventArgs e) { //默认值为false, 表示db未保存 bool flag = false; DBProcess p = new DBProcess(); DataTable dt = p.GetDataTable(false); // 改变状态并执行一个批更新 for (int i = 0; i < GridView1.Rows.Count; i++) { if ((GridView1.Rows[i].FindControl("chkDelete") as CheckBox).Checked) { dt.Rows[i].Delete(); flag = true; } else { if (dt.Rows[i]["PersonName"].ToString() != (GridView1.Rows[i].FindControl("tbName") as TextBox).Text) { if (dt.Rows[i].RowState == DataRowState.Unchanged) { dt.Rows[i].BeginEdit(); } dt.Rows[i]["PersonName"] = (GridView1.Rows[i].FindControl("tbName") as TextBox).Text; if (dt.Rows[i].RowState == DataRowState.Unchanged) { dt.Rows[i].EndEdit(); } flag = true; } if (dt.Rows[i]["PersonAddress"].ToString() != (GridView1.Rows[i].FindControl("tbAddress") as TextBox).Text) { if (dt.Rows[i].RowState == DataRowState.Unchanged) { dt.Rows[i].BeginEdit(); } dt.Rows[i]["PersonAddress"] = (GridView1.Rows[i].FindControl("tbAddress") as TextBox).Text; if (dt.Rows[i].RowState == DataRowState.Unchanged) { dt.Rows[i].EndEdit(); } flag = true; } } } p.BatchSave(dt); // 保存数据到db,不再需要保持状态颜色 HidState.Value = "[]"; dt = p.GetDataTable(true); GridView1.DataSource = dt; GridView1.DataBind(); if (flag) { ClientScript.RegisterStartupScript (GetType(), "js", "alert('保存所有更改成功!');", true); } }