<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting"
AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="10"
AutoGenerateColumns="False" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing">
<PagerSettings FirstPageText="首页" LastPageText="尾页" Mode="NextPreviousFirstLast"
NextPageText="下一页" PreviousPageText="上一页" />
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkHeader" runat="server" AutoPostBack="False" οnclick="SelectAll(this)">
</asp:CheckBox>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="NameID" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<EditItemTemplate>
<asp:TextBox ID="AgeID" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="AgeID" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" HorizontalAlign="Right" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
<script type="text/javascript">
// 多选的全选与取消
function SelectAll(tempControl) {
//将除头模板中的其它所有的CheckBox取反
var theBox = tempControl;
xState = theBox.checked;
elem = theBox.form.elements;
for (i = 0; i < elem.length; i++)
if (elem[i].type == "checkbox" && elem[i].id != theBox.id) {
if (elem[i].checked != xState)
elem[i].click();
}
}
</script>
</body>
</html>
-------------------------------------------------------C#----------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
namespace WebApplication1
{
public partial class WebForm4 : System.Web.UI.Page
{
DataTable dt = new DataTable();
private ArrayList arr = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["test"] = empty();
ViewState["arr"] = this.arr;
Bind();
}
}
//数据层
private DataTable empty()
{
int count = 0;
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Age");
for (int i = 0; i < 49; i++)
{
dt.Rows.Add(i, ++count);
}
return dt;
}
// 数据绑定
private void Bind()
{
savestate();
this.GridView1.DataSource = ViewState["test"];
this.GridView1.DataBind();
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
if (this.arr.Contains(((Label)(this.GridView1.Rows[i].FindControl("NameID"))).Text))
{
((CheckBox)this.GridView1.Rows[i].FindControl("chk")).Checked = true;
}
}
this.ViewState["arr"] = this.arr;
}
//删除数据
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt = (DataTable)ViewState["test"];
//int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
int id = GridView1.PageIndex * GridView1.PageSize + e.RowIndex;
dt.Rows.RemoveAt(id);
Bind();
}
//切换到编辑模式
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
Bind();
}
//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
Bind();
}
//分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.Bind();
}
//保存选择状态
private void savestate()
{
this.arr = (ArrayList)this.ViewState["arr"];
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
String curId = ((Label)(this.GridView1.Rows[i].FindControl("NameID"))).Text.ToString();
CheckBox chk = (CheckBox)this.GridView1.Rows[i].FindControl("chk");
if (chk.Checked && !this.arr.Contains(curId))
{
this.arr.Add(curId);
}
if (!chk.Checked && this.arr.Contains(curId))
{
this.arr.Remove(curId);
}
}
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = (DataTable)ViewState["test"];
//获取文本框的值
String NewValue = ((TextBox)(this.GridView1.Rows[e.RowIndex].FindControl("AgeID"))).Text.ToString();
//获取ID(索引)的值
//int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
int id = GridView1.PageIndex * GridView1.PageSize + e.RowIndex;
dt.Rows[id][1] = NewValue;
GridView1.EditIndex = -1;
Bind();
}
}
}