Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid/GridView - Part 2: Maintaining CheckBox State Acr

Storing and Maintaining DataGrid CheckBox Values

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?

You might also like...

Comments

About the author

Dimitrios Markatos

Dimitrios Markatos United States

Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and des...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay