热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> .Net编程>>正文内容

datagrid分页保持每页先前选择的checkbox的状态

发布时间:2009年09月18日点击数: 未知

返回教程目录

 网上很多保持分页的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();            
        }


        Web 窗体设计器生成的代码

        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();
            }

        }

    }

}

 

这样的话达到这样的效果,比如分页一个页面10个记录,
我们在第一页的时候选择了1,4,6记录,翻页到第二页
在第二页的时候选择了2,3,7记录,然后翻页回第一页
看到checkbox还是选择的是1,4,6记录,翻页到第二页选择2,3,7,每个页面独立保存自己的状态

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作