|
发表于 2020-2-22 22:00:01
|
显示全部楼层
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_2.aspx.cs" Inherits="GridView_2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView的在线编辑功能</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" DataKeyNames="id" runat="server" AllowPaging="True" AutoGenerateColumns="False"
PageSize="4" OnPageIndexChanging="GridView1_PageIndexChanging" Width="100%" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" >
<Columns>
<asp:BoundField DataField="id" HeaderText="ID编号" ReadOnly="true" />
<asp:BoundField DataField="name" HeaderText="名称" ReadOnly="true" />
<asp:ImageField DataImageUrlField="face" HeaderText="头像" ReadOnly="true" />
<asp:BoundField DataField="tel" HeaderText="电话" />
<asp:CommandField HeaderText="更新" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
注意,我们在上面声明GridView控件时的代码,OnRowCancelingEdit="GridView1_RowCancelingEdit"指定执行取消编辑的事件名称, OnRowEditing="GridView1_RowEditing"表示激活编辑的事件名称, OnRowUpdating="GridView1_RowUpdating" 表示确认更新的事件名称, OnRowDeleting="GridView1_RowDeleting"表示执行删除的事件名称。在默认的情况下,GridView在编辑状态下的所有列都是可编辑的,如果我们不允许编辑某一列,指定某列的ReadOnly="true"。这里,我们假设只允许编辑电话列,其他列都不允许编辑。
GridView_2.aspx.cs代码如下。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class GridView_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//把生成的数据表保存在Session中,
Session["DataBase"] = CreatDataBase();
//调用绑定方法
GridViewDataBind();
}
}
//数据绑定
protected void GridViewDataBind()
{
DataTable dt = (DataTable)Session["DataBase"];
//指定GridView1控件的数据源
this.GridView1.DataSource = dt;
//调用GridView1控件的DataBind方法
this.GridView1.DataBind();
}
//生成数据表,数据表是数据绑定控件的数据源
protected DataTable CreatDataBase()
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("id", typeof(int));
dt.Columns.Add(dc);
dc = new DataColumn("name", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("tel", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("email", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("face", typeof(string));
dt.Columns.Add(dc);
int i = 1;
DataRow dr;
for (i = 1; i < 10; i++)
{
dr = dt.NewRow();
dr["id"] = i;
dr["name"] = "Name . " + i.ToString() + " .";
dr["tel"] = "+86-755-8839-3207";
dr["email"] = "Email_" + i.ToString() + "@hotmail.com";
dr["face"] = "./images/" + i.ToString() + ".gif";
dt.Rows.Add(dr);
}
return dt;
}
// GridView控件的翻页事件
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
GridViewDataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
GridViewDataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
GridViewDataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// 当前操作行 ,当前操作行的Index=e.RowIndex
GridViewRow dvr = GridView1.Rows[e.RowIndex];
//获取更新的单元格
TableCell tc = dvr.Cells[3];
//单元格内的TextBox控件。
TextBox tb = (TextBox)tc.Controls[0];
//获取TextBox控件的Txext,就是更新后的值
string NewValue = tb.Text;
//获取数据表中的ID键值
string id = dvr.Cells[0].Text;
/*-----------------------------------
* 上面获取id的方法 等价于string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
* 虽然上面的方法比较繁琐,但是这个方法可以获取其他列的内容,
* 也可以获取没个单元格内其他控件的属性,这个在负责的GridView表中尤其有用
*---------------------------------------------------
* */
DataTable dt = (DataTable)Session["DataBase"];
foreach (DataRow dr in dt.Rows)
{
//找到原数据,更新
if (dr["id"].ToString() == id)
{
dr["tel"] = NewValue;
break;
}
}
//将更新后的数据保存到Seesion中,实际运用中,我们将保存到数据库中
Session["DataBase"] = dt;
//还原编辑状态
this.GridView1.EditIndex = -1;
//刷新数据
GridViewDataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
DataTable dt = (DataTable)Session["DataBase"];
foreach (DataRow dr in dt.Rows)
{
//找到原数据,更新
if (dr["id"].ToString() == id)
{
dt.Rows.Remove(dr);
break;
}
}
//将更新后的数据保存到Seesion中,实际运用中,我们将保存到数据库中
Session["DataBase"] = dt;
//刷新数据
GridViewDataBind();
}
}
|
|