实现gridview里面的全选操作 protected void chkSelectAll_CheckedChanged(object sender, EventArgs e) { //全选操作 for (int i = 0; i < dgdShowData.Rows.Count; i++) { //CheckBox ch = (CheckBox)dgdShowData.Rows[i].FindControl("CheckBox1"); //ch.Checked=true; ((CheckBox)dgdShowData.Rows[i].FindControl("CheckBox1")).Checked = ((CheckBox)sender).Checked; } } 转到另外一个页面,传递一个付负号的参数里面转到另外一个页面可以进行添加操作 protected void btnAdd_Click(object sender, EventArgs e) { Response.Redirect("EmployeeDetails.aspx?id=-1"); } 批量删除 protected void btnDeleteAll_Click(object sender, EventArgs e) { //批量删除 string ids = ""; for (int i = 0; i < dgdShowData.Rows.Count; i++) { if (((CheckBox)dgdShowData.Rows[i].FindControl("CheckBox1")).Checked) ids += dgdShowData.DataKeys[i].Value + ","; } BLL.EmployeeBLL empBLL = new BLL.EmployeeBLL(); ids = ids.Substring(0, ids.Length - 1); lblMsg.Text = empBLL.DeleteEmployee(ids); BindGrid(); } 执行sql /// <summary> /// 返回受影响的行数:在此完成所有的Insert、Update、Delete /// </summary> /// <param name="sqlQuery">SQL语句</param> /// <returns>受影响的行数: /// -2:数据库连接异常 /// -1:数据库操作异常 /// </returns> public int ExecQuery(string sqlQuery) { using (SqlConnection cn = new SqlConnection(dbStr)) { SqlCommand cm = new SqlCommand(sqlQuery, cn); try { cn.Open(); try { return cm.ExecuteNonQuery(); } catch (Exception) { return -1; } } catch (Exception) { return -2; } finally { cm.Dispose(); } } } 保存 protected void btnSave_Click(object sender, EventArgs e) { BLL.EmployeeBLL empBLL = new BLL.EmployeeBLL(); Model.Employee emp = new Model.Employee(); emp.ID = int.Parse(Request.QueryString["id"]); emp.Name = txtName.Text.Trim(); emp.Gender = radNan.Checked ? "男" : "女"; emp.Birthday = DateTime.Parse(txtBirthday.Text); emp.Phone = txtPhone.Text; emp.Email = txtEmail.Text; emp.DepID = int.Parse(dropDep.SelectedValue); if (emp.ID != -1) lblMsg.Text = empBLL.UpdateEmployee(emp); else lblMsg.Text = empBLL.InsertEmployee(emp); }