界面设计:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default15.aspx.cs" Inherits="Default15" %>
<!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>Excel 导入</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" OnPageIndexChanging="GridView1_PageIndexChanging"
AllowPaging="true" OnDataBinding="GridView1_DataBinding" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="全选" Width="106px" />
<asp:Button ID="Button2" runat="server" Text="单页全选" Width="106px"
οnclick="Button2_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Default15 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GvDataBind();
}
}
//绑定GridView数据源
public void GvDataBind()
{
SqlConnection con = new SqlConnection("Data Source=192.168.0.110;DataBase=erp;uid=sa;pwd=123");
con.Open();
try
{
SqlCommand cmd = new SqlCommand("select userid,username,enname from t_user", con);
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);
GridView1.DataSource = dt.DefaultView;
GridView1.DataKeyNames = new string[] { "userid" };
GridView1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
//分页事件
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GvDataBind();
}
protected List<string> SelectedItems
{
get { return ViewState["selecteditems"] != null ? (List<string>)ViewState["selecteditems"] : null; }
set { ViewState["selecteditems"] = value; }
}
private void GetSelectedItem()
{
List<string> selecteditems = null;
if (this.SelectedItems == null)
{
selecteditems = new List<string>();
}
else
{
selecteditems = this.SelectedItems;
}
//获取选择的记录
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
CheckBox cbx = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
string id = this.GridView1.DataKeys[i].Values[0].ToString();
if (selecteditems.Contains(id) && !cbx.Checked)
{
selecteditems.Remove(id);
}
if (!selecteditems.Contains(id) && cbx.Checked)
{
selecteditems.Add(id);
}
}
this.SelectedItems = selecteditems;
}
//全选按钮
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
GvDataBind();
foreach (GridViewRow drRow in GridView1.Rows)
{
CheckBox CheckBox1 = (CheckBox)drRow.Cells[0].FindControl("CheckBox1");
if (CheckBox1.Checked)
{
CheckBox1.Checked = false;
}
else
{
CheckBox1.Checked = true;
}
}
GridView1.AllowPaging = true;
GvDataBind();
}
//GridView1 行绑定
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex > -1 && this.SelectedItems != null)
{
CheckBox cbx = (CheckBox)e.Row.FindControl("CheckBox1");
string id = this.GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString();
if (SelectedItems.Contains(id))
{
cbx.Checked = true;
}
else
{
cbx.Checked = false;
}
}
}
}
//数据绑定
protected void GridView1_DataBinding(object sender, EventArgs e)
{
GetSelectedItem();
}
//单页全选、反选
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow drRow in GridView1.Rows)
{
CheckBox CheckBox1 = (CheckBox)drRow.Cells[0].FindControl("CheckBox1");
if (CheckBox1.Checked)
{
CheckBox1.Checked = false;
}
else
{
CheckBox1.Checked = true;
}
}
}
}