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


In my previous article Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid (i.e. HotMail & Yahoo) that I had written about two years ago, I demonstrated how one could multi-select checkboxes in a DataGrid page, and then cumulatively deleted them all. Initially, I did not make use of any paging with that example, but rather left it rudimentary simply to illustrate how to multi-select a series of checkboxes and then delete them all in one pass. But the limitation there was that this worked on a per-page basis, so once you selected any given checkboxes in that page, then paged to the next, and finally returned to the previous page, all your selections were reset.

Most of the web sites that offer this type of functionality, including AOL Web Mail, display the same abovementioned effect. You're allow only to select and delete whatever checkboxes are presented on that page alone, before proceeding to the next. Having said that, we’ll take care of this in this article, and not only be able to select and delete across pages in one nice batch delete, but also, and here is the cool part, in this article I’ll show you how to repopulate the given page with the precise checkboxes that were selected earlier - even if you go back and check something new in that page.

To make this all come about I utilize Session State to store my checkboxed values that comprise of the DataGrid's DataKeyField and, use its DataKeys properties to sift through and grab the selected checkbox values and repopulate the appropriate checkboxes in the given page. I have also implemented caching using Session State for added performance and scalability, alongside bi-directional column sorting. Therefore, by means of the DataGrid's DataKeys property, your checkbox selections will persist across pages even sorting in any direction as well. Pretty awesome! And furthermore, the use of Session State helps prevent multiple users from getting data crisscrossed.

Typically, there are other methodologies for accomplishing the same end result. One way I've come across was by means of wiring up each CheckBox with an event handler using OnCheckedChanged. Thereby, each time a given checkbox is selected the page get's posted back and stores the appropriate values in any given state capturing method. However, although this technique works, it leaves much to be desired it terms of smoothness and presentation. My method performs all actions on a page by page level, and only when paging occurs is when it'll do what's it supposed to do. In turn, you have a smooth application with non of the constant post back effect for each checkbox selection.

Moving forward, for brevity I won’t reiterate the techniques discussed in Part 1 of this article, but rather I’ll begin by listing the code in its entirety (both the main page and its code-behind), then discuss the logic and code responsible for the effect. Incidentally, as the original code was written in C#, most readers typically requested from me the VB version, so in light of such, the code presented here will all be in VB.

You might also like...


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.


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.

“Before software should be reusable, it should be usable.” - Ralph Johnson