Question Maintain Grid View Checkbox while paging

cthang

Member
Joined
Feb 10, 2009
Messages
8
Programming Experience
Beginner
Hi,

I am using Visual studios 2008 with VB as the code behind. I have checkboxes in my gridview and also paging. When I check a few items and then go to the next page and come back the checkboxes are unchecked.

Is there anyway to keep the checkboxes checked while you area paging through the gridview?

Thanks
 

cthang

Member
Joined
Feb 10, 2009
Messages
8
Programming Experience
Beginner
Here is the code I have so far. The problem I am having is the Protected Sub GridView1_PageIndexChanging.

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
 
Top Bottom