When page porperty changes then add the IDs to viewState
protected void AddRowstoIDList()
{
foreach (ListViewDataItem lvi in lvExample.Items)
{
CheckBox chkSelect = (CheckBox)lvi.FindControl("chkSelect");
if ((((chkSelect) != null)))
{
int ID = Convert.ToInt32(lvExample.DataKeys[lvi.DisplayIndex].Value);
//Check if the ID is already there
if ((chkSelect.Checked && !this.IDs.Contains(ID)))
{
this.IDs.Add(ID);
}
else if ((!chkSelect.Checked && this.IDs.Contains(ID)))
{
this.IDs.Remove(ID);
}
}
}
}
When page property changes(going to another page) then add the IDs of previously checked checkboxes
protected void lvExample_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
AddRowstoIDList();
}
In ListView Item Databound display the previously checked checkboxes according to the IDs stored in the Viewstate while databinding
protected void lvExample_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem lvi = (ListViewDataItem)e.Item;
if (lvi.ItemType == ListViewItemType.DataItem)
{
// Get each checkbox Listview Item on DataBound
CheckBox chkSelect = (CheckBox)lvi.FindControl("chkSelect");
// Make sure we're referencing the correct control
if ((((chkSelect) != null)))
{
// If the ID exists in our list then check the checkbox
int ID = Convert.ToInt32(lvExample.DataKeys[lvi.DisplayIndex].Value);
chkSelect.Checked = this.IDs.Contains(ID);
}
}
}