一.说明
部分代码的运用放在以往的教程中,本部分只讲解删除/更新功能
二.前端
我们在其控件上添加事件:
红色为行删除事件
绿色为行更新事件
双击后,即可在后台自动生成对应的方法体
其代码显示:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebGv2.aspx.cs" Inherits="WebApplication6.WebGv2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%--AllowPaging:True 数据进行分页
PageSize:3 每页分'3'行
OnPageIndexChanging: 页码改变事件
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
DataKeyNames:id 主键(数据表的列名,按钮事件需要)
OnRowCancelingEdit: 取消修改事件
OnRowEditing: 修改事件
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
本节知识点
OnRowDeleting: 行删除事件
OnRowUpdating: 行更新事件
--%>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="3" DataKeyNames="id" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
三.后端代码
我们填充删除与更新的方法体:
/// <summary>
/// 行删除事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e">删除事件</param>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//GridView1:当前使用的控件
//DataKeys:主键[]
//e.RowIndex 删除行的索引
string sql = string.Format(@"delete from student where id = '{0}'",GridView1.DataKeys[e.RowIndex].Value.ToString());
//用行封装方法,将数据删除
MyDBSql.excuteNonQuery(sql);
//-1 表示编辑取消,因为我们上面将数据删除了,所以前端显示刷新后,自然显示数据已删除
GridView1.EditIndex = -1;
bind();
}
/// <summary>
/// 更新事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string sql ="update student set name='{0}',sex={1} where id='{2}'";
//定义变量:
//GridView1.Rows[e.RowIndex]数据表格的当前编辑行
//Cells[2] 编辑行的第几列,因为我们的编辑/删除按钮也算一行,id也算一行,所以我们从第2行开始抓取数据
//Controls 类似表格中的文本框
//先定位控件的当前编辑行,然后是编辑行的第几列,然后是第几列的文本控件,然后将其转换为TextBox类型
//然后取得TextBox 的Text ,并取消空格,转换为string类型
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim().ToString();
int sex = ((CheckBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Checked ? 1 : 0;
//获取id和删除获取id的方式一样
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
sql = string.Format(sql,name,sex,id);
//用行封装方法,将数据修改
MyDBSql.excuteNonQuery(sql);
//取消编辑状态,并重新加载数据
GridView1.EditIndex = -1;
bind();
}
我们点击更新后,数据库便自动更新,同时页面重新加载,显示已更新的数据
四.数据库封装方法
这里我们调用简单的修改/删除方法:,不明白其原理的小伙伴可以翻看小白以前的笔记.ASP.net 连接Mysql,封装添加功能,原理也差不多是一样的,都是一些简单的方法,相信大家学习起来并部困难
public static int excuteNonQuery(string sql) {
conn.Open();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
int result=-1;
try {
result=cmd.ExecuteNonQuery();
}
finally
{
cmd.Dispose();
conn.Close();
}
return result;
}
五.知识点
- 前端asp:GridView 属性:
AllowPaging:True 数据进行分页
PageSize:3 每页分'3'行
OnPageIndexChanging: 页码改变事件
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
DataKeyNames:id 主键(数据表的列名,按钮事件需要)
OnRowCancelingEdit: 取消修改事件
OnRowEditing: 修改事件
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
本节知识点
OnRowDeleting: 行删除事件
OnRowUpdating: 行更新事件
- 行删除用到的参数
//GridView1:当前使用的控件
//DataKeys:主键[]
//e.RowIndex 获取删除行的索引
//Value.ToString() 获取值,并转换为string类型
string sql = string.Format(@"delete from student where id = '{0}'",GridView1.DataKeys[e.RowIndex].Value.ToString());
- 行删除注意事项
//在数据库删除完毕后,取消组件删除状态,并重新加载数据
GridView1.EditIndex = -1;
//此方法是自己写的方法,重新加载数据,不知道的小伙伴可以查看上节笔记的内容
bind();
- 行修改(更新)事件参数
//定义变量:
//GridView1.Rows[e.RowIndex]数据表格的当前编辑行
//Cells[2] 编辑行的第几列,因为我们的编辑/删除按钮也算一行,id也算一行,所以我们从第2行开始抓取数据
//Controls 类似表格中的文本框
//先定位控件的当前编辑行,然后是编辑行的第几列,然后是第几列的文本控件,然后将其转换为TextBox类型
//然后取得TextBox 的Text ,并取消空格,转换为string类型
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim().ToString();
//当然我们也可以按需求,转换不同的控件,和类型
int sex = ((CheckBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Checked ? 1 : 0;
- 行更新同样需要取消编辑状态,并重新加数据
//取消编辑状态,并重新加载数据
GridView1.EditIndex = -1;
bind();
六.一些补充
<asp:GridView ID="maintainView" runat="server" AutoGenerateColumns="False"
EnableModelValidation="True" DataKeyNames="id" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
>
<Columns>
<asp:BoundField DataField="id" HeaderText="编号" />
<asp:BoundField DataField="name" HeaderText="命名" />
<asp:BoundField DataField="capa_group" HeaderText="Capa_Group" />
<asp:BoundField DataField="wip1" HeaderText="wip1" />
<asp:BoundField DataField="wip3" HeaderText="wip3" />
<asp:BoundField DataField="out" HeaderText="out" />
<asp:BoundField DataField="state" HeaderText="state" />
<asp:BoundField DataField="qty" HeaderText="qty" />
<%--将编辑设置为按钮,并且标注名称--%>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
CancelText="取消" DeleteText="删除" EditText="编辑" InsertText="插入" NewText="新建"
SelectText="选择" UpdateText="更新" ButtonType="Button" />
</Columns>
<HeaderStyle BackColor="Yellow" Font-Bold="true" ForeColor="BurlyWood" />
<%--编辑行--%>
<EditRowStyle BackColor="YellowGreen" />
<%--页脚--%>
<FooterStyle BackColor="ButtonFace" Font-Bold="true" ForeColor="BurlyWood" />
</asp:GridView>