views:

2758

answers:

2

I have an ASP.NET application that displays various views into a largish database of protein sequences. As a visitor browses the data, I'd like for them to be able to select a CheckBox in the GridView row to flag sequences for later download as a zipped text file. I don't want to store the selections so they should just be valid for the session.

My idea right now is to add a TemplateField to the GridView and then add a CheckBox to that. I intend to handle the check events and store the sequence IDs in the session state. When the user heads to the download page I'll get the session data, display the list of sequences they are about to download and then send the file along. Obviously I'll also have to reparse the session data on every form load / page switch.

So I actually have a couple of questions about this:

1) Am I doing too much work? Is there an easier way to implement this?

2) Are all the round trips to the server for the session state likely to be a performance problem? I can put a "Save for Download" button on the page to batch it to help things. Handling the check events just seems more error tolerant since you can't accidentally lose your state if you navigate away.

3) Is it possible to sort the GridView by the checked box column? I'd like to sort by the checked box column first then the currently sorted column (e.g. If the GridView is sorted by last name, sorting by the check column and then by last name).

In case it matters, I'm using C#, .NET 3.5, VS2008 and I'm using the simple drag-n-drop way of creating a GridView from a SQLServerExpress table.

A: 

I'm not sure there's an easier way to do it, but as for your other questions:

2.) You need to decide for yourself the balance between robustness and network traffic. If you use the check event, you're going to go back to the server quite a bit. If you use a different event (losing focus on the grid, an explicit "update/save" button click, or navigating to another page), you cut back on traffic at the expense of possibly losing information. It's a UI design decision with no "right" answer.

3.) It's possible, though it may not be automatic. I know that DataGridViews will generate handlers to sort by checkbox columns automatically. I'm not sure it's that easy with the regular GridView. Is there a reason you're using a GV and not a DGV?

Coderer
The DataGridView is Windows Forms only. System.Web.UI.WebControls only has the DataGrid and the GridView.
Dana Robinson
+1  A: 

Use AJAX via a Page Method to keep track of the checkbox status in the session state. This will minimize the round-trip cost.

I don't think you'll be able to sort by the check box in the GridView as you need to specify the sort column. You may be able to do this by making a synthetic data source that basically joins your saved session information with the data from the table but not by using a SQLDataSource connected directly to the table.

tvanfosson