Now, the logic behind maintaining current pages values and recalling them in the current page all takes place for the most part in the DataGrid Paging function, where in between paging and databinding two things occur. One, is to find out which checkboxes were selected prior the DataGrid being paged - this being done by the GetCheckBoxValues()
method listed below. And the other, is to store them by means of Session State.
The code presented below is what parses the DataGrid's checkboxes and determines which ones are selected, if any, using the FindControl method. It then holds the selected values via the DataGrid's DataKeyField property for each checkbox selected and adds them all into an ArrayList, which in turn is added to Session State.
Sub GetCheckBoxValues() 'As paging occurs store checkbox values
CheckedItems = New ArrayList
'Loop through DataGrid Items
For Each dgItem In MyDataGrid.Items
'Retrieve key value of each record based on DataGrids
' DataKeyField property
ChkBxIndex = MyDataGrid.DataKeys(dgItem.ItemIndex)
CheckBox = dgItem.FindControl("DeleteThis")
'Add ArrayList to Session if it doesnt exist
If Not IsNothing(Session ("CheckedItems")) Then
CheckedItems = Session ("CheckedItems")
End If
If CheckBox.Checked Then
BxChkd = True
'Add to Session if it doesnt already exist
If Not CheckedItems.Contains(ChkBxIndex) Then
CheckedItems.Add(ChkBxIndex.ToString())
End If
Else
'Remove value from Session when unchecked
CheckedItems.Remove(ChkBxIndex.ToString())
End If
Next
'Update Session with the list of checked items
Session ("CheckedItems") = CheckedItems
End Sub
Now that we've discussed the methodology for storing the selected boxes. What about paging back and forth and checking boxes arbitrarily and sorting, won't this whack the order and such out of place? Nope. For instance, if you select any two checkboxes on the first page, then page to the next, the second you page back your page is repopulated with your previously selected values. So if you remove them all, then page again, the aforementioned method deletes the values in the Session State, so when you page back, they're gone.
But say you didn't uncheck any, but rather checked off a couple more, then paged again. Well, same the logic applies here as well, we easily determine upon paging, one, that you already have existing values in Session State for the given page, which in truth is the ID field and not necessarily the page itself. And two, all that is now left to do is update Session State with the new ID values.
The function responsible for delegating the actions at the appropriate time resides in the DataGrid paging event handler method. Here it where it determines which checkboxes were selected, then it does it thing. After Databind has occurred we call the RePopulateCheckBoxes()
method to tell us if the given page we've paged to has checkboxes it needs to repopulate.
Sub MyDataGrid_Page (sender As Object, e As DataGridPageChangedEventArgs)
'Get CheckBoxValues before paging occurs
GetCheckBoxValues()
MyDataGrid.CurrentPageIndex = e.NewPageIndex
BindData(Session ("SortOrder"))
'Populate current DataGrid page with the current page items from Session after databind
RePopulateCheckBoxes ()
End Sub
Once all this happens you have each DataGrid page filled with the correct boxes checked or empty. Pretty cool! Now, how does one repopulate the checkboxes on any page?
Comments