DataGrid分页显示复选框选中

我们都曾做过DataGrid的分页实现,使用.NET中的控件非常简单,但如果在DataGrid中添加一个模板列,内部放一个CheckBox的话, 转到下一页再转回则会发现原来选中的内容已经没有了!以下的演示是在.NET2005中使用GridView控件实现的分页后实现复选框多选功能,请先观 察代码!

 

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:GridView ID="gvEmployee" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            OnPageIndexChanging="gvEmployee_PageIndexChanging" PageSize="7">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chk" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="EmployeeID" HeaderText="员工编号" />
                <asp:BoundField DataField="LastName" HeaderText="名" />
                <asp:BoundField DataField="FirstName" HeaderText="姓" />
                <asp:BoundField DataField="Title" HeaderText="称谓" />
                <asp:BoundField DataField="BirthDate" HeaderText="出生日期" />
            </Columns>
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>

Default.aspx.cn
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;

using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    private ArrayList arr = new ArrayList();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.ViewState.Add("arr", this.arr);
            this.Bind();
        }
    }

    private void Bind()
    {
        SqlConnection cn = new SqlConnection("SERVER=.;DATABASE=Northwind;UID=sa;PWD=;");
        SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Employees",cn);
        DataSet ds = new DataSet();
        sda.Fill(ds, "emp");

        this.gvEmployee.DataSource = ds.Tables[0];
        this.gvEmployee.DataBind();

        for (int i = 0; i < this.gvEmployee.Rows.Count; i++)
        {
            if (this.arr.Contains(this.gvEmployee.Rows[i].Cells[1].Text))
            {
                ((CheckBox)this.gvEmployee.Rows[i].FindControl("chk")).Checked = true;
            }
        }
        this.ViewState["arr"] = this.arr;
    }

    protected void gvEmployee_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.arr = (ArrayList)this.ViewState["arr"];
        for (int i = 0; i < this.gvEmployee.Rows.Count; i++)
        {
            string curId = this.gvEmployee.Rows[i].Cells[1].Text;
            CheckBox chk = (CheckBox)this.gvEmployee.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);
            }
        }
        this.gvEmployee.PageIndex = e.NewPageIndex;
        this.Bind();
    }
}

注 意:由于在.NET2005中的类是分部类,自动生成的代码没有拷贝出,所以订阅代码自己去双击!实现原理是每次在页面替换时使用一个ArrayList 集合将所有选中项保存,然后每次重新加载页面时到此集合中查找,若有则设置复选框为选中,否则略过!ArrayList集合放在视图状态中比较简单!请仔 细观察代码逻辑,在.NET2003中的DataGrid的使用完全相同!
PS:注释不写了,代码很简单,请自行研究!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值