1
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_CheckBoxColumn.aspx.cs" Inherits="GridSamples_GridView_CheckBoxColumn"
%>
2
3 <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
4
5 < html xmlns ="http://www.w3.org/1999/xhtml" >
6 < head runat ="server" >
7 < title > 无标题页 </ title >
8 < script language ="javascript" type ="text/javascript" >
9 function selectAll(obj)
10 {
11 var theTable = obj.parentElement.parentElement.parentElement;
12 var i;
13 var j = obj.parentElement.cellIndex;
14
15 for(i=0;i<theTable.rows.length;i++)
16 {
17 var objCheckBox = theTable.rows[i].cells[j].firstChild;
18 if(objCheckBox.checked!=null)objCheckBox.checked = obj.checked;
19 }
20 }
21 </ script >
22 </ head >
23 < body >
24 < form id ="form1" runat ="server" >
25 < div >
26 < asp:GridView ID ="GridView1" runat ="server" AllowPaging ="True" AutoGenerateColumns ="False"
27 DataKeyNames ="id" DataSourceID ="AccessDataSource1" AllowSorting ="True" OnDataBinding ="GridView1_DataBinding" OnRowDataBound ="GridView1_RowDataBound" >
28 < Columns >
29 < asp:TemplateField >
30 < ItemTemplate >
31 < asp:CheckBox ID ="CheckBox1" runat ="server" Checked ="True" Text ='<%#DataBinder.Eval(Container.DataItem,"id") % > ' />
32 </ ItemTemplate >
33 < HeaderTemplate >
34 < input id ="CheckAll" type ="checkbox" onclick ="selectAll(this);" /> 本页全选
35 </ HeaderTemplate >
36 </ asp:TemplateField >
37 < asp:BoundField DataField ="id" HeaderText ="id" InsertVisible ="False" ReadOnly ="True"
38 SortExpression ="id" />
39 < asp:BoundField DataField ="name" HeaderText ="name" SortExpression ="name" />
40 < asp:BoundField DataField ="sex" HeaderText ="sex" SortExpression ="sex" />
41 < asp:BoundField DataField ="deptid" HeaderText ="deptid" SortExpression ="deptid" />
42 </ Columns >
43 </ asp:GridView >
44
45
46 < asp:AccessDataSource ID ="AccessDataSource1" runat ="server" DataFile ="~/App_Data/test.mdb"
47 SelectCommand ="SELECT [id], [name], [sex], [deptid] FROM [employees]" ></ asp:AccessDataSource >
48 < asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="ShowAllSelectedItem" />
49 < asp:TextBox ID ="TextBox1" runat ="server" Width ="200px" ></ asp:TextBox ></ div >
50 </ form >
51 </ body >
52 </ html >
53
2
3 <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
4
5 < html xmlns ="http://www.w3.org/1999/xhtml" >
6 < head runat ="server" >
7 < title > 无标题页 </ title >
8 < script language ="javascript" type ="text/javascript" >
9 function selectAll(obj)
10 {
11 var theTable = obj.parentElement.parentElement.parentElement;
12 var i;
13 var j = obj.parentElement.cellIndex;
14
15 for(i=0;i<theTable.rows.length;i++)
16 {
17 var objCheckBox = theTable.rows[i].cells[j].firstChild;
18 if(objCheckBox.checked!=null)objCheckBox.checked = obj.checked;
19 }
20 }
21 </ script >
22 </ head >
23 < body >
24 < form id ="form1" runat ="server" >
25 < div >
26 < asp:GridView ID ="GridView1" runat ="server" AllowPaging ="True" AutoGenerateColumns ="False"
27 DataKeyNames ="id" DataSourceID ="AccessDataSource1" AllowSorting ="True" OnDataBinding ="GridView1_DataBinding" OnRowDataBound ="GridView1_RowDataBound" >
28 < Columns >
29 < asp:TemplateField >
30 < ItemTemplate >
31 < asp:CheckBox ID ="CheckBox1" runat ="server" Checked ="True" Text ='<%#DataBinder.Eval(Container.DataItem,"id") % > ' />
32 </ ItemTemplate >
33 < HeaderTemplate >
34 < input id ="CheckAll" type ="checkbox" onclick ="selectAll(this);" /> 本页全选
35 </ HeaderTemplate >
36 </ asp:TemplateField >
37 < asp:BoundField DataField ="id" HeaderText ="id" InsertVisible ="False" ReadOnly ="True"
38 SortExpression ="id" />
39 < asp:BoundField DataField ="name" HeaderText ="name" SortExpression ="name" />
40 < asp:BoundField DataField ="sex" HeaderText ="sex" SortExpression ="sex" />
41 < asp:BoundField DataField ="deptid" HeaderText ="deptid" SortExpression ="deptid" />
42 </ Columns >
43 </ asp:GridView >
44
45
46 < asp:AccessDataSource ID ="AccessDataSource1" runat ="server" DataFile ="~/App_Data/test.mdb"
47 SelectCommand ="SELECT [id], [name], [sex], [deptid] FROM [employees]" ></ asp:AccessDataSource >
48 < asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="ShowAllSelectedItem" />
49 < asp:TextBox ID ="TextBox1" runat ="server" Width ="200px" ></ asp:TextBox ></ div >
50 </ form >
51 </ body >
52 </ html >
53
1
using
System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class GridSamples_GridView_CheckBoxColumn : System.Web.UI.Page
13 {
14 /**//// <summary>
15 /// 获取或设置选中项的集合
16 /// </summary>
17 protected ArrayList SelectedItems
18 {
19 get
20 {
21 return (ViewState["mySelectedItems"] != null) ? (ArrayList)ViewState["mySelectedItems"] : null;
22 }
23 set
24 {
25 ViewState["mySelectedItems"] = value;
26 }
27 }
28
29 protected void Page_Load(object sender, EventArgs e)
30 {
31
32 }
33
34
35 protected void GridView1_DataBinding(object sender, EventArgs e)
36 {
37 //在每一次重新绑定之前,需要调用CollectSelected方法从当前页收集选中项的情况
38 CollectSelected();
39 }
40
41
42 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
43 {
44 //这里的处理是为了回显之前选中的情况
45 if (e.Row.RowIndex > -1 && this.SelectedItems!=null)
46 {
47 DataRowView row = e.Row.DataItem as DataRowView;
48 CheckBox cb = e.Row.FindControl("CheckBox1") as CheckBox;
49 if(this.SelectedItems.Contains(row["id"].ToString()))
50 cb.Checked = true;
51 else
52 cb.Checked = false;
53 }
54 }
55 /**//// <summary>
56 /// 从当前页收集选中项的情况
57 /// </summary>
58 protected void CollectSelected()
59 {
60 ArrayList selectedItems = null;
61 if (this.SelectedItems == null)
62 selectedItems = new ArrayList();
63 else
64 selectedItems = this.SelectedItems;
65
66 for (int i = 0; i < this.GridView1.Rows.Count; i++)
67 {
68 string id = this.GridView1.Rows[i].Cells[1].Text;
69 CheckBox cb = this.GridView1.Rows[i].FindControl("CheckBox1") as CheckBox;
70 if (selectedItems.Contains(id) && !cb.Checked)
71 selectedItems.Remove(id);
72 if (!selectedItems.Contains(id) && cb.Checked)
73 selectedItems.Add(id);
74 }
75 this.SelectedItems = selectedItems;
76 }
77
78 protected void Button1_Click(object sender, EventArgs e)
79 {
80 //最后,需要对选中项进行操作之前,不能忘了还要最后一次收集当前页的选中情况
81 CollectSelected();
82
83 this.TextBox1.Text = string.Empty;
84 foreach (object tmp in this.SelectedItems)
85 this.TextBox1.Text += tmp.ToString() + ",";
86 }
87}
88
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class GridSamples_GridView_CheckBoxColumn : System.Web.UI.Page
13 {
14 /**//// <summary>
15 /// 获取或设置选中项的集合
16 /// </summary>
17 protected ArrayList SelectedItems
18 {
19 get
20 {
21 return (ViewState["mySelectedItems"] != null) ? (ArrayList)ViewState["mySelectedItems"] : null;
22 }
23 set
24 {
25 ViewState["mySelectedItems"] = value;
26 }
27 }
28
29 protected void Page_Load(object sender, EventArgs e)
30 {
31
32 }
33
34
35 protected void GridView1_DataBinding(object sender, EventArgs e)
36 {
37 //在每一次重新绑定之前,需要调用CollectSelected方法从当前页收集选中项的情况
38 CollectSelected();
39 }
40
41
42 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
43 {
44 //这里的处理是为了回显之前选中的情况
45 if (e.Row.RowIndex > -1 && this.SelectedItems!=null)
46 {
47 DataRowView row = e.Row.DataItem as DataRowView;
48 CheckBox cb = e.Row.FindControl("CheckBox1") as CheckBox;
49 if(this.SelectedItems.Contains(row["id"].ToString()))
50 cb.Checked = true;
51 else
52 cb.Checked = false;
53 }
54 }
55 /**//// <summary>
56 /// 从当前页收集选中项的情况
57 /// </summary>
58 protected void CollectSelected()
59 {
60 ArrayList selectedItems = null;
61 if (this.SelectedItems == null)
62 selectedItems = new ArrayList();
63 else
64 selectedItems = this.SelectedItems;
65
66 for (int i = 0; i < this.GridView1.Rows.Count; i++)
67 {
68 string id = this.GridView1.Rows[i].Cells[1].Text;
69 CheckBox cb = this.GridView1.Rows[i].FindControl("CheckBox1") as CheckBox;
70 if (selectedItems.Contains(id) && !cb.Checked)
71 selectedItems.Remove(id);
72 if (!selectedItems.Contains(id) && cb.Checked)
73 selectedItems.Add(id);
74 }
75 this.SelectedItems = selectedItems;
76 }
77
78 protected void Button1_Click(object sender, EventArgs e)
79 {
80 //最后,需要对选中项进行操作之前,不能忘了还要最后一次收集当前页的选中情况
81 CollectSelected();
82
83 this.TextBox1.Text = string.Empty;
84 foreach (object tmp in this.SelectedItems)
85 this.TextBox1.Text += tmp.ToString() + ",";
86 }
87}
88
posted on