GridView分页时维护CheckBox选择状态

View Code
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.Data.SqlClient;

namespace GridView分页时维护CheckBox选择状态
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBinds();
            }
        }

        //绑定数据
        protected void DataBinds()
        {
            gvBookList.DataSource = DBHelper.GetDataTable("select * from books");
            gvBookList.DataBind();
        }

        //保存CheckBox状态
        protected void RememberOldValues()
        {
            //将选中行的ID存放在List中
            List<int> list = null;

            int index = -1;
            //判断是否有选中的状态信息
            if (ViewState["Check_Items"] != null)
            {
                list = ViewState["Check_Items"] as List<int>;
            }
            else //如果没有则新建一个
            {
                list = new List<int>(); 
            }

            //遍历GridView所有行
            foreach (GridViewRow row in gvBookList.Rows)
            {
                //取出设置为主键列的值
                index = (int)gvBookList.DataKeys[row.RowIndex].Value;
                //获取当前行复选框是否被选中
                bool result = ((CheckBox)row.FindControl("cbSelect")).Checked;
                //如果选中且在列表中不存在则添加
                if (result)
                {
                    if (!list.Contains(index))
                    {
                        list.Add(index);
                    }
                }
                else//如果未选中,在列表框中存在则删除
                {
                    if (list.Contains(index))
                    {
                        list.Remove(index);
                    }
                }
            }

            //保存选中状态
            ViewState["Check_Items"] = list;
        }

        //分页
        protected void gvBookList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            RememberOldValues();//保存当前页选中状态
            gvBookList.PageIndex=e.NewPageIndex;
            DataBinds();
            RePopulateValues();//还原当前页选中状态
        }

        //还原CheckBox状态
        protected void RePopulateValues()
        {
            //判断是否有选中状态数据
            if (ViewState["Check_Items"] != null)
            {
                //取出状态数据
                List<int> list = ViewState["Check_Items"] as List<int>;

                foreach(GridViewRow row in gvBookList.Rows)
                {
                    //取出该页中所有行的主键列数据
                    int index=(int)gvBookList.DataKeys[row.RowIndex].Value;
                    //判断该行主键的值在列表中是否存在,存在设置为选中
                    if(list.Contains(index))
                    {
                        CheckBox cbSelect =
                            row.FindControl("cbSelect") as CheckBox;
                        cbSelect.Checked = true;
                    }
                }
            }
        }

        //全选引发的事件
        protected void cbSelAll_CheckedChanged(object sender, EventArgs e)
        {
            //获取当前按钮
            CheckBox cbSelAll= sender as CheckBox;
            //设置全选/全不选
            foreach(GridViewRow row in gvBookList.Rows)
            {
                CheckBox cbSelect = row.FindControl("cbSelect") as CheckBox;
                cbSelect.Checked = cbSelAll.Checked;
            }
        }
    }
}

 

posted @ 2012-06-25 10:47  ComBat  阅读(194)  评论(0编辑  收藏  举报