GridView翻页时保持CheckBox选择状态

//定义翻页bool
1 None.gif private bool  changed = false ;
//定义保存选择项主键集合属性
1 None.gif protected  List < string >  SelectedItems
2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
3ExpandedSubBlockStart.gifContractedSubBlock.gif       get dot.gifreturn ViewState["selecteditems"]!=null ? (List<string>)ViewState["selecteditems"] : null; }
4ExpandedSubBlockStart.gifContractedSubBlock.gif        set dot.gif{ ViewState["selecteditems"= value; }
5ExpandedBlockEnd.gif    }
//获取选择项主键
 1 None.gif private   void  GetSelectedItem()
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gif List<string> selecteditems = null;
 4InBlock.gif if (this.SelectedItems == null)
 5ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
 6InBlock.gif    selecteditems = new List<string>();
 7ExpandedSubBlockEnd.gif }

 8InBlock.gif else
 9ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
10InBlock.gif    selecteditems = this.SelectedItems;
11ExpandedSubBlockEnd.gif }

12InBlock.gif
13InBlock.gif //获取选择的记录
14InBlock.gif     for (int i = 0; i < this.gvwResult.Rows.Count; i++)
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16InBlock.gif           CheckBox cbx = (CheckBox)this.gvwResult.Rows[i].FindControl("chkSelect");
17InBlock.gif
18InBlock.gif            string id = this.gvwResult.DataKeys[i].Values[0].ToString();
19InBlock.gif
20InBlock.gif            if (selecteditems.Contains(id) && !cbx.Checked)
21InBlock.gif                selecteditems.Remove(id);
22InBlock.gif            if (!selecteditems.Contains(id) && cbx.Checked)
23InBlock.gif                selecteditems.Add(id);
24ExpandedSubBlockEnd.gif        }

25InBlock.gif        this.SelectedItems = selecteditems;
26ExpandedBlockEnd.gif}
//在 DataBinding事件中执行GetSelectedItem()
1 None.gif protected   void  gvwResult_DataBinding( object  sender, EventArgs e)
2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
3InBlock.gif        GetSelectedItem();
4InBlock.gif    changed=true;
5ExpandedBlockEnd.gif    }
//在行绑定时确定CheckBox选中状态
 1 None.gif protected   void  gvwResult_RowDataBound( object  sender, GridViewRowEventArgs e)
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {      
 3InBlock.gif        if (e.Row.RowIndex > -1 && this.SelectedItems!= null)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{           
 5InBlock.gif            CheckBox cbx = (CheckBox)e.Row.FindControl("chkSelect");
 6InBlock.gif            string id = this.gvwResult.DataKeys[e.Row.RowIndex].Values[0].ToString();
 7InBlock.gif            if (SelectedItems.Contains(id))
 8InBlock.gif                cbx.Checked = true;
 9InBlock.gif            else
10InBlock.gif                cbx.Checked = false;
11ExpandedSubBlockEnd.gif        }

12ExpandedBlockEnd.gif    }
//根据changed确定是否再执行GetSelectedItem(),否则不翻页时选择项获取不到.
1 None.gif protected   void  btnConfirm_Click( object  sender, EventArgs e)
2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
3InBlock.gif      if(!changed)
4InBlock.gif        GetSelectedItem();
5InBlock.gif      foreach (string id in (List<string>)this.SelectedItems)
6ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
7InBlock.gif                  //dot.gif..
8ExpandedSubBlockEnd.gif                 }

9ExpandedBlockEnd.gif     }

转载于:https://www.cnblogs.com/jiangshaofen/archive/2007/06/13/782450.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值