GridView加入CheckBox实现跨页CheckBox全选

转载 2007年09月30日 16:48:00

转:

http://www.cnblogs.com/diction/archive/2007/09/26/906127.html 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_CheckBoxColumn.aspx.cs" Inherits="GridSamples_GridView_CheckBoxColumn" %>

<!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>
       <script language="javascript" type="text/javascript">
       function selectAll(obj)
       {
           var theTable     = obj.parentElement.parentElement.parentElement;
           var i;
           var j = obj.parentElement.cellIndex;
        
           for(i=0;i<theTable.rows.length;i++)
           {
               var objCheckBox = theTable.rows[i].cells[j].firstChild;
               if(objCheckBox.checked!=null)objCheckBox.checked = obj.checked;
           }
       }
       </script>
</head>
<body>
       <form id="form1" runat="server">
       <div>
           <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
               DataKeyNames="id" DataSourceID="AccessDataSource1" AllowSorting="True" OnDataBinding="GridView1_DataBinding" OnRowDataBound="GridView1_RowDataBound">
               <Columns>
                   <asp:TemplateField>
                       <ItemTemplate>
                           <asp:CheckBox ID="CheckBox1" runat="server" Checked="True" Text='<%#DataBinder.Eval(Container.DataItem,"id") %>' />
                       </ItemTemplate>
                       <HeaderTemplate>
                           &nbsp;<input id="CheckAll" type="checkbox" onclick="selectAll(this);" />本页全选
                       </HeaderTemplate>
                   </asp:TemplateField>
                   <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                       SortExpression="id" />
                   <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                   <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
                   <asp:BoundField DataField="deptid" HeaderText="deptid" SortExpression="deptid" />
               </Columns>
           </asp:GridView>
           &nbsp;
      
           <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/test.mdb"
               SelectCommand="SELECT [id], [name], [sex], [deptid] FROM [employees]"></asp:AccessDataSource>
           <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ShowAllSelectedItem" />
           <asp:TextBox ID="TextBox1" runat="server" Width="200px" ReadOnly="True"></asp:TextBox></div>
       </form>
</body>
</html>

************************************
************************************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class GridSamples_GridView_CheckBoxColumn : System.Web.UI.Page
{
       /// <summary>
       /// 获取或设置选中项的集合
       /// </summary>
       protected ArrayList SelectedItems
       {
           get
           {
               return (ViewState["mySelectedItems"] != null) ? (ArrayList)ViewState["mySelectedItems"] : null;
           }
           set
           {
               ViewState["mySelectedItems"] = value;
           }
       }

       protected void Page_Load(object sender, EventArgs e)
       {
        
       }


       protected void GridView1_DataBinding(object sender, EventArgs e)
       {
           //在每一次重新绑定之前,需要调用CollectSelected方法从当前页收集选中项的情况
           CollectSelected();
       }

    
       protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           //这里的处理是为了回显之前选中的情况
           if (e.Row.RowIndex > -1 && this.SelectedItems!=null)
           {
               DataRowView row = e.Row.DataItem as DataRowView;
               CheckBox cb = e.Row.FindControl("CheckBox1") as CheckBox;
               if(this.SelectedItems.Contains(row["id"].ToString()))
                   cb.Checked = true;
               else
                   cb.Checked = false;
           }
       }
       /// <summary>
       /// 从当前页收集选中项的情况
       /// </summary>
       protected void CollectSelected()
       {
           ArrayList selectedItems = null;
           if (this.SelectedItems == null)
               selectedItems = new ArrayList();
           else
               selectedItems = this.SelectedItems;

           for (int i = 0; i < this.GridView1.Rows.Count; i++)
           {
               string id = this.GridView1.Rows[i].Cells[1].Text;
               CheckBox cb = this.GridView1.Rows[i].FindControl("CheckBox1") as CheckBox;
               if (selectedItems.Contains(id) && !cb.Checked)
                   selectedItems.Remove(id);
               if (!selectedItems.Contains(id) && cb.Checked)
                   selectedItems.Add(id);
           }
           this.SelectedItems = selectedItems;
       }

       protected void Button1_Click(object sender, EventArgs e)
       {
           //最后,需要对选中项进行操作之前,不能忘了还要最后一次收集当前页的选中情况
           CollectSelected();

           this.TextBox1.Text = string.Empty;
           foreach (object tmp in this.SelectedItems)
               this.TextBox1.Text += tmp.ToString() + ",";
       }

相关文章推荐

C# GridView列头添加CheckBox控件实现全选功

给DevExpress GridView的选择列Header添加CheckBox实现对所有数据行的全选功能 看图: 显示选择行的资料: /// /// C...

GridView控件中模板列CheckBox全选、反选、取消实现代码

using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using ...
  • ysghyy
  • ysghyy
  • 2011年11月24日 20:38
  • 254

GridView中CheckBox实现全选或反选功能的方法

方法一:添加一个模板列TemplateField.然后,切换到源代码进行后续的编辑.在GridView控件中刚刚生成的 标签对之间,添加一个标签对和标签对.我们利用在列头处显示一个checkbox,用...
  • yl_99
  • yl_99
  • 2011年06月03日 15:07
  • 9444

C#_winform_GridView_CheckBox_多选_全选

  • 2017年01月12日 09:55
  • 52KB
  • 下载

asp.net gridview中添加checkbox列,用于全部选中记录或取消全选

方法一:使用javascript代码控制                          前台代码:...
  • nvhaixx
  • nvhaixx
  • 2013年10月08日 14:03
  • 4173

操作gridview,选中行时改变背景色且checkbox被选中,隔行变色,全选变色

css: .checked { background: #c0c0ff; } .odd { background: #72FE95; } .even { backgrou...

Flex中DataGrid实现列(包括头部)中加入CheckBox全选功能

CheckBoxHeaderRenderer.as view plain package    {              import flash.events.Event;     ...

js实现checkbox 全选和取消

  • 2014年07月16日 17:29
  • 921B
  • 下载

关于Repeater中嵌套GridView以及GridView中checkbox的全选功能的实例

具体的思路为:首先生成一个Repeater,然后根据在Repeater绑定的ID生成GridView,具体实现界面及代码如下所示: 前台代码: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:GridView加入CheckBox实现跨页CheckBox全选
举报原因:
原因补充:

(最多只允许输入30个字)