一.说明
部分代码的运用放在以往的教程中,本部分只讲解删除/更新功能
二.前端
我们在其控件上添加事件:
红色为行删除事件
绿色为行更新事件
双击后,即可在后台自动生成对应的方法体
其代码显示:
<%@ 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>
三.后端代码
我们填充删除与更新的方法体:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sql = string.Format(@"delete from student where id = '{0}'",GridView1.DataKeys[e.RowIndex].Value.ToString());
MyDBSql.excuteNonQuery(sql);
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string sql ="update student set name='{0}',sex={1} where id='{2}'";
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;
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;
}
五.知识点
AllowPaging:True 数据进行分页
PageSize:3 每页分'3'行
OnPageIndexChanging: 页码改变事件
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
DataKeyNames:id 主键(数据表的列名,按钮事件需要)
OnRowCancelingEdit: 取消修改事件
OnRowEditing: 修改事件
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
本节知识点
OnRowDeleting: 行删除事件
OnRowUpdating: 行更新事件
string sql = string.Format(@"delete from student where id = '{0}'",GridView1.DataKeys[e.RowIndex].Value.ToString());
GridView1.EditIndex = -1;
bind();
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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)