<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridView跨页多选._Default" %>
<!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>
<script type="text/javascript">
//这是使用JavaScript
function Check(ck)
{
if(ck.checked)
{
form1.Hidden1.value += ","+ck.value;
}
else
{
form1.Hidden1.value = form1.Hidden1.value.replace(","+ck.value,"");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
GridLines="None" Height="184px"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowDataBound="GridView1_RowDataBound" PageSize="2" Width="515px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<input ID="ltName" runat="server" name="ltName" onclick="Check(this)"
type="checkbox" value='<%# Eval("name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="学生ID">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="name" HeaderText="学生姓名">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="sex" HeaderText="性别">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="times" HeaderText="入学时间">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<br />
<asp:Button ID="btnGet" runat="server" Text="获取选择内容" onclick="btnGet_Click" />
<br />
<asp:Label ID="lbShow" runat="server"></asp:Label>
<br />
<input id="Hidden1" type="hidden" runat="server"/>
</div>
</form>
</body>
</html>
后台
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace GridView跨页多选
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
Bind();
}
protected void btnGet_Click(object sender, EventArgs e)
{
lbShow.Text = Hidden1.Value.Replace(",", "<li>");
}
protected void Bind()
{
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123456;database=Test;");
SqlDataAdapter da = new SqlDataAdapter("select * from students", con);
DataSet ds = new DataSet();
da.Fill(ds, "students");
this.GridView1.DataSource = ds.Tables["students"].DefaultView;
this.GridView1.DataBind();
}
//保证翻页时复选框仍然选中
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Hidden1.Value.IndexOf(e.Row.Cells[2].Text) >= 0)
{
HtmlInputCheckBox box = (HtmlInputCheckBox)(e.Row.Cells[0].FindControl("ltName"));
box.Checked = true;
}
}
}
}
}