GridView中添加一个CheckBox列

 

 1 ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_CheckBoxColumn.aspx.cs" Inherits="GridSamples_GridView_CheckBoxColumn"  %>
 2 None.gif
 3 None.gif <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
 4 None.gif
 5 None.gif < html  xmlns ="http://www.w3.org/1999/xhtml"   >
 6 None.gif < head  runat ="server" >
 7 None.gif     < title > 无标题页 </ title >
 8 ExpandedBlockStart.gifContractedBlock.gif     < script  language ="javascript"  type ="text/javascript" > dot.gif
 9InBlock.gif    function selectAll(obj)
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11InBlock.gif        var theTable  = obj.parentElement.parentElement.parentElement;
12InBlock.gif        var i;
13InBlock.gif        var j = obj.parentElement.cellIndex;
14InBlock.gif        
15InBlock.gif        for(i=0;i<theTable.rows.length;i++)
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            var objCheckBox = theTable.rows[i].cells[j].firstChild;
18InBlock.gif            if(objCheckBox.checked!=null)objCheckBox.checked = obj.checked;
19ExpandedSubBlockEnd.gif        }

20ExpandedSubBlockEnd.gif    }

21ExpandedBlockEnd.gif    
</ script >
22 None.gif </ head >
23 None.gif < body >
24 None.gif     < form  id ="form1"  runat ="server" >
25 None.gif     < div >
26 None.gif         < asp:GridView  ID ="GridView1"  runat ="server"  AllowPaging ="True"  AutoGenerateColumns ="False"
27 None.gif            DataKeyNames ="id"  DataSourceID ="AccessDataSource1"  AllowSorting ="True"  OnDataBinding ="GridView1_DataBinding"  OnRowDataBound ="GridView1_RowDataBound" >
28 None.gif             < Columns >
29 None.gif                 < asp:TemplateField >
30 None.gif                     < ItemTemplate >
31 None.gif                         < asp:CheckBox  ID ="CheckBox1"  runat ="server"  Checked ="True"  Text ='<%#DataBinder.Eval(Container.DataItem,"id")  % > ' />
32 None.gif                     </ ItemTemplate >
33 None.gif                     < HeaderTemplate >
34 None.gif                         &nbsp; < input  id ="CheckAll"  type ="checkbox"  onclick ="selectAll(this);"   /> 本页全选
35 None.gif                     </ HeaderTemplate >
36 None.gif                 </ asp:TemplateField >
37 None.gif                 < asp:BoundField  DataField ="id"  HeaderText ="id"  InsertVisible ="False"  ReadOnly ="True"
38 None.gif                    SortExpression ="id"   />
39 None.gif                 < asp:BoundField  DataField ="name"  HeaderText ="name"  SortExpression ="name"   />
40 None.gif                 < asp:BoundField  DataField ="sex"  HeaderText ="sex"  SortExpression ="sex"   />
41 None.gif                 < asp:BoundField  DataField ="deptid"  HeaderText ="deptid"  SortExpression ="deptid"   />
42 None.gif             </ Columns >
43 None.gif         </ asp:GridView >
44 None.gif         &nbsp;
45 None.gif      
46 None.gif         < asp:AccessDataSource  ID ="AccessDataSource1"  runat ="server"  DataFile ="~/App_Data/test.mdb"
47 None.gif            SelectCommand ="SELECT [id], [name], [sex], [deptid] FROM [employees]" ></ asp:AccessDataSource >
48 None.gif         < asp:Button  ID ="Button1"  runat ="server"  OnClick ="Button1_Click"  Text ="ShowAllSelectedItem"   />
49 None.gif         < asp:TextBox  ID ="TextBox1"  runat ="server"  Width ="200px" ></ asp:TextBox ></ div >
50 None.gif     </ form >
51 None.gif </ body >
52 None.gif </ html >
53 None.gif

 1 None.gif using  System;
 2 None.gif using  System.Data;
 3 None.gif using  System.Configuration;
 4 None.gif using  System.Collections;
 5 None.gif using  System.Web;
 6 None.gif using  System.Web.Security;
 7 None.gif using  System.Web.UI;
 8 None.gif using  System.Web.UI.WebControls;
 9 None.gif using  System.Web.UI.WebControls.WebParts;
10 None.gif using  System.Web.UI.HtmlControls;
11 None.gif
12 None.gif public  partial  class  GridSamples_GridView_CheckBoxColumn : System.Web.UI.Page
13 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
14ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
15InBlock.gif    /// 获取或设置选中项的集合
16ExpandedSubBlockEnd.gif    /// </summary>

17InBlock.gif    protected ArrayList SelectedItems
18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
19InBlock.gif        get
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            return (ViewState["mySelectedItems"!= null? (ArrayList)ViewState["mySelectedItems"] : null;
22ExpandedSubBlockEnd.gif        }

23InBlock.gif        set
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            ViewState["mySelectedItems"= value;
26ExpandedSubBlockEnd.gif        }

27ExpandedSubBlockEnd.gif    }

28InBlock.gif
29InBlock.gif    protected void Page_Load(object sender, EventArgs e)
30ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
31InBlock.gif        
32ExpandedSubBlockEnd.gif    }

33InBlock.gif
34InBlock.gif
35InBlock.gif    protected void GridView1_DataBinding(object sender, EventArgs e)
36ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
37InBlock.gif        //在每一次重新绑定之前,需要调用CollectSelected方法从当前页收集选中项的情况
38InBlock.gif        CollectSelected();
39ExpandedSubBlockEnd.gif    }

40InBlock.gif
41InBlock.gif    
42InBlock.gif    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
43ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
44InBlock.gif        //这里的处理是为了回显之前选中的情况
45InBlock.gif        if (e.Row.RowIndex > -1 && this.SelectedItems!=null)
46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
47InBlock.gif            DataRowView row = e.Row.DataItem as DataRowView;
48InBlock.gif            CheckBox cb = e.Row.FindControl("CheckBox1"as CheckBox;
49InBlock.gif            if(this.SelectedItems.Contains(row["id"].ToString()))
50InBlock.gif                cb.Checked = true;
51InBlock.gif            else
52InBlock.gif                cb.Checked = false;
53ExpandedSubBlockEnd.gif        }

54ExpandedSubBlockEnd.gif    }

55ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
56InBlock.gif    /// 从当前页收集选中项的情况
57ExpandedSubBlockEnd.gif    /// </summary>

58InBlock.gif    protected void CollectSelected()
59ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
60InBlock.gif        ArrayList selectedItems = null;
61InBlock.gif        if (this.SelectedItems == null)
62InBlock.gif            selectedItems = new ArrayList();
63InBlock.gif        else
64InBlock.gif            selectedItems = this.SelectedItems;
65InBlock.gif
66InBlock.gif        for (int i = 0; i < this.GridView1.Rows.Count; i++)
67ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
68InBlock.gif            string id = this.GridView1.Rows[i].Cells[1].Text;
69InBlock.gif            CheckBox cb = this.GridView1.Rows[i].FindControl("CheckBox1"as CheckBox;
70InBlock.gif            if (selectedItems.Contains(id) && !cb.Checked)
71InBlock.gif                selectedItems.Remove(id);
72InBlock.gif            if (!selectedItems.Contains(id) && cb.Checked)
73InBlock.gif                selectedItems.Add(id);
74ExpandedSubBlockEnd.gif        }

75InBlock.gif        this.SelectedItems = selectedItems;
76ExpandedSubBlockEnd.gif    }

77InBlock.gif
78InBlock.gif    protected void Button1_Click(object sender, EventArgs e)
79ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
80InBlock.gif        //最后,需要对选中项进行操作之前,不能忘了还要最后一次收集当前页的选中情况
81InBlock.gif        CollectSelected();
82InBlock.gif
83InBlock.gif        this.TextBox1.Text = string.Empty;
84InBlock.gif        foreach (object tmp in this.SelectedItems)
85InBlock.gif            this.TextBox1.Text += tmp.ToString() + ",";
86ExpandedSubBlockEnd.gif    }

87ExpandedBlockEnd.gif}

88 None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值