Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 62,305 times

Contents

Related Categories

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

DMarko1

Repopulate our DataGrid

This is all brought about in a more or less similar manner as storing the values. The RePopulateCheckBoxes()method listed below starts of by setting up an ArrayList based on the values stored in Session State. Next, as before, it loops through the current page DataGrids items, and if Session State contains any matching values that happen to be on the current page, it'll then proceed and set the appropriate checkbox to true.

Sub RePopulateCheckBoxes ()

CheckedItems = New ArrayList

CheckedItems = Session ("CheckedItems")

If Not IsNothing(CheckedItems) Then

'Loop through DataGrid Items
For Each dgItem in MyDataGrid.Items

ChkBxIndex = MyDataGrid.DataKeys(dgItem.ItemIndex)

'Repopulate DataGrid with items found in Session
If CheckedItems.Contains(ChkBxIndex) Then

CheckBox = CType(dgItem.FindControl("DeleteThis"), CheckBox)
CheckBox.Checked = True

End If

Next

End If

'Copy ArrayList to a new array
Results = CheckedItems.ToArray(GetType(String))

'Concatenate ArrayList with comma to properly send for deletion
deletedIds = String.Join(",", Results)

End Sub

Additionally, the aforesaid method at the end also copies the ArrayList into a new array from which the values get cleanly concatenated with commas, so they can be batch deleted.

The rest of our code is pretty undemanding. The remaining functions are all self explanatory. For one, you have the SortOrder() function, which properly sorts the order based on the values passed to it, in turn sorting the DataGrid accordingly. The other notable function is the DeleteAllIds() method that deletes all the values that were setup in the method listed above. Moreover, since one might uncheck any checkbox on the given current page, then proceed to delete their selections, we need to support this action by including the GetCheckBoxValues() and RePopulateCheckBoxes() methods, whereby it'll regrab the values and update Session State accordingly, so the final delete reflect this change.

Finally, you have the ClearDataGrid() method, that willclear all Session State values and rebinds our DataGrid from the top. And that all she wrote.

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 desktop applications. Till now Jimmy has authored nearly two dozen .NET articles, published on Dot Net Junkies, 4 Guys From Rolla, Sitepoint, MSDN Academic Alliance, Developers.NET, The Official Microsoft ASP.NET Site, and here on Developer Fusion, covering various unique and advanced techniques on .NET.

Comments