//Namespaces needed
using System.Collections;
/// <summary>
/// method to retrieve all the selected items in the GridView
/// NOTE: This is so we dont lose track of them while paging
/// </summary>
private void GetCheckedItems(string crtl,GridView grdView)
{
//instantiate new ArrayList to hold our checked items
ArrayList checkedItems =
new ArrayList
();
CheckBox chk;
string chkBoxIndex = string.Empty;
//loop through each row in the GridView
foreach (GridViewRow row in grdView.Rows)
{
//get the index of the current CheckBox
chkBoxIndex = (string)grdView.DataKeys[row.RowIndex].Value.ToString();
chk = (CheckBox)row.FindControl(ctrl);
//add ArrayList to Session is if doesnt already exist
if (!(Session["CheckedItems"] == null))
{
checkedItems = (ArrayList)Session["CheckedItems"];
}
//now see if the current CheckBox is checked
if (chk.Checked)
{
//see if the current value is in the Session, if not add it
if (!(checkedItems.Contains(chkBoxIndex)))
{
//add to the list
checkedItems.Add(chkBoxIndex);
}
else
{
//remove from list since it's unchecked
checkedItems.Remove(chkBoxIndex);
}
}
}
//update Session with the list of checked items
Session["CheckedItems"] = checkedItems;
}