public ArrayList SelectedItems
{
get
{ return (ViewState["SelectedItems"] != null) ? (ArrayList)ViewState["SelectedItems"] : null; }
set
{ ViewState["SelectedItems"] = value; }
}
protected void gvList_DataBinding(object sender, EventArgs e)
{ //在每一次重新绑定之前,需要调用CollectSelected方法从当前页收集选中项的情况
CollectSelected();
}
//从当前页收集选中项的情况
public void CollectSelected()
{
ArrayList selectedItems = null;
if (SelectedItems == null)
selectedItems = new ArrayList();
else
selectedItems = SelectedItems;
for (int i = 0; i < gvList.Rows.Count; i++)
{
string recordid = gvList.DataKeys[i]["recordid"].ToString();
CheckBox cb = (CheckBox)gvList.Rows[i].Cells[0].FindControl("cbcheck");
if (selectedItems.Contains(recordid) && !cb.Checked)
selectedItems.Remove(recordid);
if (!selectedItems.Contains(recordid) && cb.Checked)
selectedItems.Add(recordid);
}
SelectedItems = selectedItems;
}
protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
{//这里的处理是为了回显之前选中的情况
if (e.Row.RowIndex > -1 && SelectedItems != null)
{
CheckBox cb = (CheckBox)e.Row.Cells[0].FindControl("cbcheck");
DataRowView drv = (DataRowView)e.Row.DataItem;
if (SelectedItems.Contains(drv["recordid"].ToString()))
cb.Checked = true;
else
cb.Checked = false;
}
}
protected void ibtnok_Click(object sender, ImageClickEventArgs e)
{
CollectSelected();
foreach(object obj in SelectedItems)
{
System.Windows.Forms.MessageBox.Show(obj.ToString());
}
}