Private Sub RememberOldValues()
Dim categoryIDList As New ArrayList()
Dim index As Integer = -1
For Each row As GridViewRow In GridView1.Rows
index = CInt(GridView1.DataKeys(row.RowIndex).Value)
Dim result As Boolean = DirectCast(row.FindControl("CheckBox1"), CheckBox).Checked
' Check in the Session
If Session("CHECKED_ITEMS") IsNot Nothing Then
categoryIDList = DirectCast(Session("CHECKED_ITEMS"), ArrayList)
End If
If result Then
If Not categoryIDList.Contains(index) Then
categoryIDList.Add(index)
End If
Else
categoryIDList.Remove(index)
End If
Next
If categoryIDList IsNot Nothing AndAlso categoryIDList.Count > 0 Then
Session("CHECKED_ITEMS") = categoryIDList
End If
End Sub
Private Sub RePopulateValues()
Dim categoryIDList As ArrayList = DirectCast(Session("CHECKED_ITEMS"), ArrayList)
If categoryIDList IsNot Nothing AndAlso categoryIDList.Count > 0 Then
For Each row As GridViewRow In GridView1.Rows
Dim index As Integer = CInt(GridView1.DataKeys(row.RowIndex).Value)
If categoryIDList.Contains(index) Then
Dim myCheckBox As CheckBox = DirectCast(row.FindControl("CheckBox1"), CheckBox)
myCheckBox.Checked = True
End If
Next
End If
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
RememberOldValues()
GridView1.PageIndex = e.NewPageIndex
DataBind()
RePopulateValues()
End Sub