GridView中添加一个CheckBox列,翻页后保存选中状态


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

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

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

28ExpandedSubBlockEnd.gif    }

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

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

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

55ExpandedSubBlockEnd.gif    }

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

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

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

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

88ExpandedBlockEnd.gif}

89 None.gif

转载于:https://www.cnblogs.com/shineqiujuan/archive/2008/09/02/1281740.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值