转:http://lovecherry.cnblogs.com/archive/2005/10/18/257338.html

网上很多保持分页的datagird的checkbox选择状态的文章实现的是保存当前页面的chexkbox所选,也就是说第一页选择了第一条记录翻页到第二页后还是选择第一条记录,然后选择了第二条记录,再去看第一页还是第二条记录,保存的只是上次操作的结果,而我们往往希望分开保存所有页面的选择情况,下面是示例代码:

前台:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="checktest.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>WebForm1</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
        <LINK href="css.css" type="text/css" rel="stylesheet">
    </HEAD>
    <body>
        <form id="Form1" method="post" runat="server">
            <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" CellSpacing="1" BorderWidth="0px"
                CellPadding="5" CssClass="border" AllowPaging="True" PageSize="10">
                <ItemStyle CssClass="item"></ItemStyle>
                <HeaderStyle CssClass="header"></HeaderStyle>
                <Columns>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:CheckBox ID="chk" Runat="server"></asp:CheckBox>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:BoundColumn DataField="CustomerID" HeaderText="CustomerID"></asp:BoundColumn>
                    <asp:BoundColumn DataField="CompanyName" HeaderText="CompanyName"></asp:BoundColumn>
                    <asp:BoundColumn DataField="ContactTitle" HeaderText="ContactTitle"></asp:BoundColumn>
                </Columns>
                <PagerStyle CssClass="header" Mode="NumericPages"></PagerStyle>
            </asp:datagrid>
            <asp:Button id="Button1" runat="server" Text="清空记录"></asp:Button>
        </form>
    </body>
</HTML>

后台:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace checktest
{
    public class WebForm1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Button Button1;
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            if(!IsPostBack)
            {
                SetBind();                
            }
        }
        
        private void SetBind()
        {
            SqlConnection conn=new SqlConnection("server=(local);uid=sa;pwd=sa
;database=Northwind");
            SqlDataAdapter da=new SqlDataAdapter("select * from Customers",conn);
            DataSet ds=new DataSet();
            da.Fill(ds,"table1");
            this.DataGrid1.DataSource=ds.Tables["table1"];
            this.DataGrid1.DataBind();            
        }

#region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {    
            this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
            this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Load += new System.EventHandler(this.Page_Load);

}
        #endregion

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
        {
            string data="";
            foreach(DataGridItem dgi in this.DataGrid1.Items)
            {
                CheckBox cb=(CheckBox)dgi.FindControl("chk");
                if(cb.Checked)
                    data+="1";
                else
                    data+="0";
            }

if(ViewState["pagedata"]!=null)
            {
                Hashtable ht=(Hashtable)ViewState["pagedata"];
                if(ht.Contains(this.DataGrid1.CurrentPageIndex))                
                    ht[this.DataGrid1.CurrentPageIndex]=data;
                else
                    ht.Add(this.DataGrid1.CurrentPageIndex,data);
                ViewState["pagedata"]=ht;
            }
            else
            {
                Hashtable ht=new Hashtable();
                ht.Add(this.DataGrid1.CurrentPageIndex,data);
                ViewState["pagedata"]=ht;
            }
            this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
            SetBind();
        }

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
            {
                if(ViewState["pagedata"]!=null)
                {
                    Hashtable ht=(Hashtable)ViewState["pagedata"];
                    if(ht.Contains(this.DataGrid1.CurrentPageIndex))
                    {
                        CheckBox cb=(CheckBox)e.Item.FindControl("chk");
                        cb.Checked=ht[this.DataGrid1.CurrentPageIndex].ToString()[e.Item.ItemIndex].ToString()=="1";
                    }
                }
            }
        }

private void Button1_Click(object sender, System.EventArgs e)
        {
            if(ViewState["pagedata"]!=null)
            {
                Hashtable ht=new Hashtable();
                ViewState["pagedata"]=ht;
                SetBind();
            }
        }
    }
}

转载于:https://www.cnblogs.com/Dragon-China/archive/2007/02/28/659080.html

如何datagrid分页保持每页先前选择的checkbox的状态?相关推荐

  1. datagrid分页问题(前后跳页)《控件版》

    在ASCX中写的. 在CSDN上看了很多的DATAGRID分页问题,当然DATAGRID有自己的分页项,功能是很有限的,我也在CSDN上看了很多自己分页的代码,发现都是用C#写的,我写了一个用ASP. ...

  2. jquery easyui datagrid 分页 详解

    http://www.cnblogs.com/huozhicheng/archive/2011/09/27/2193605.html 由于项目原因,用了jquery easyui 感觉界面不错,皮肤样 ...

  3. EasyUI之简单实现Datagrid分页(C#)

    EasyUI之简单实现Datagrid分页(C#) 刚刚开始学着用EasyUI,有兴趣的TX可以去http://www.jeasyui.com/看一下,当然很多人都知道,不是什么新鲜的东西: 这两天在 ...

  4. easyui php分页,easyui datagrid分页 4、easyUI-七种布局(layout)

    1.为网页创建边框布局 边框布局(border layout)提供五个区域:east.west.north.south.center.以下是一些通常用法: north 区域可以用来显示网站的标语. s ...

  5. DataGrid分页;指定列的总和和平均值;显示鼠标背景色;弹出式窗口;

    1 在页脚中添加指定列的总和和平均值 private void dgitem_ItemDataBound() {    if(e.Item.ItemIndex >=0)      {       ...

  6. 使用easyUI datagrid分页

    使用easyUI datagrid分页 我用了比较简单的sturts2和mybatis结合的小测试demo实现的. 一.使用easyUI datagrid分页注意事项及原理: 1.EasyUI的Dat ...

  7. Capture原理图分页符跨页标注

    Capture原理图分页符跨页标注 选中原理图DSN,右键选择Annotate 在弹出的界面选择Add Intersheet References 输入你期望的标号的摆放位置,选择OK 标号前网络名旁 ...

  8. oracle数据库如何写翻页_ORACLE数据库分页查询/翻页 最佳实践

    ORACLE数据库分页查询/翻页 最佳实践 一.示例数据: Select Count(*) From dba_objects ; ----------------------------------- ...

  9. datagrid分页传递参数_四类数据库分页实现方案总结之Mysql分页实现

    概述 昨天介绍了Oracle分页实现方案,那么,mysql又是如何实现分页呢? 参考官网:https://dev.mysql.com/doc/refman/5.7/en/select.html mys ...

最新文章

  1. mysql数据库-主主配置
  2. python删除类方法_python中向类中动态添加新特性及删除属性方法
  3. python矩形语句_浅谈Python3实现两个矩形的交并比(IoU)
  4. (原)产品化:架构、过程管理
  5. java的equals方法_Java Date equals()方法与示例
  6. pl sql代码提示手动提示设置
  7. bio-linux_Bio-Linux:稳定,可移植的科学研究Linux发行版
  8. 介绍全新的 JSX 转换
  9. mysql行级锁 select for update
  10. linux内核 image,linux内核zImage详解
  11. 程序员职业生涯系列:关于技术能力的思考与总结
  12. 《世界500强企业员工的50条生存法则》(Yanlz+Unity+SteamVR+5G+AI+VR云游戏+生存法则+潜规则+提升竞争力+术业有专攻+卓越理念+立钻哥哥+==)
  13. centos7 查看内存使用
  14. 米家?华为?阿里?Homekit?有没有你在用的智能家居平台?
  15. 宫崎骏动画里的新垣结衣见过没?这个开源动漫生成器让你的照片秒变手绘日漫
  16. oracle小麦苗博客,《Oracle DBA工作笔记》第一章
  17. 1.C语言变量类型、全局变量、局部变量
  18. python sklearn的训练集测试集根据标签的比例来切分数据集
  19. 成功将不支持网络的USB打印机变成网络打印机
  20. 百度OCR文字识别教程(有demo)

热门文章

  1. 【童年回忆】【FC模拟器 + ROM大合集下载】
  2. 不出家门也能喝上原汁原味的泰国国汤——冬阴功汤
  3. 前端差异化项目合并打包
  4. linux下mysql源码安装
  5. 字符串阵列分别输出元素的索引,原值和长度
  6. 配置Configuration Manager站点和层次架构(1)
  7. spring与springMVC配置扫描的问题
  8. Struts2自定义类型转换器、自定义拦截器和用户输入数据的验证
  9. (android硬件应用实战)摄像头拍照实现和总结
  10. JAVA命令运行cmd命令得到的结果乱码Runtime.getRuntime().exec();