本文的C#实现CheckBox翻页选中方法,从OnPageIndexChanged事件和OnItemDataBound事件分别描述,好好理解呦。
C#实现CheckBox翻页选中
- //OnPageIndexChanged事件
- string data = "";
- foreach (DataGridItem dgi in DataGrid1.Items)
- {
- CheckBox cb = (CheckBox)dgi.FindControl("chk");
- if (cb.Checked)
- {
- data += "1";
- }
- else
- {
- data += "0";
- }
- }
- if (ViewState["pagedata"] != null)
- {
- Hashtable ht = (Hashtable)ViewState["pagedata"];
- if (ht.Contains(DataGrid1.CurrentPageIndex))
- {
- ht[DataGrid1.CurrentPageIndex] = data;
- }
- else
- {
- ht.Add(DataGrid1.CurrentPageIndex, data);
- }
- ViewState["pafedata"] = ht;
- }
- else
- {
- Hashtable ht = new Hashtable();
- ht.Add(DataGrid1.CurrentPageIndex, data);
- ViewState["pagedata"] = ht;
- }
- //OnItemDataBound事件
- //查看ViewState的data是否为1从而是否选中CheckBox
- if (ViewState["pagedata"] != null)
- {
- Hashtable ht = (Hashtable)ViewState["pagedata"];
- if (ht.Contains(DataGrid1.CurrentPageIndex))
- {
- CheckBox cb = (CheckBox)e.Item.FindControl("chk");
- cb.Checked = ht[DataGrid1.CurrentPageIndex].ToString()[e.Item.ItemIndex].ToString() == "1";
- }
- }
以上就是笔者为大家介绍的C#实现CheckBox翻页选中事件。
【编辑推荐】