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

本文介绍如何在ASP.NET的GridView控件中使用Checkbox来记录用户的选中状态,并通过CollectSelected方法实现对选中项的有效管理和操作。文章详细解释了在页面加载、数据绑定及点击事件中更新和维护选中项集合的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 2None.gifusing System;
 3None.gifusing System.Data;
 4None.gifusing System.Configuration;
 5None.gifusing System.Collections;
 6None.gifusing System.Web;
 7None.gifusing System.Web.Security;
 8None.gifusing System.Web.UI;
 9None.gifusing System.Web.UI.WebControls;
10None.gifusing System.Web.UI.WebControls.WebParts;
11None.gifusing System.Web.UI.HtmlControls;
12None.gif
13None.gifpublic partial class GridSamples_GridView_CheckBoxColumn : System.Web.UI.Page
14ExpandedBlockStart.gifContractedBlock.gifdot.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}

89None.gif

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值