Combining the DataGrid and DetailsView
Web pages in data-driven web applications can pretty much be reduced down to two types: list pages and edit pages. If you can view a list of objects, create a new object, and update and delete existing objects, then you have all the components of a CRUD (create, read, update, delete). If you are a web developer, you have probably spent countless hours creating and re-creating similar pages to do these four tasks. Some upcoming tools (like BLINQ) look to help in this regard, but for now we are more or less on our own.
The introduction of the DataGrid in ASP.NET 2.0 was an exciting event. It took care of three steps of the CRUD for us: It can list objects, edit them, and delete them. Unfortunately it lacks any kind of create. For create operations, you have to redirect to a page with a DetailsView. And, truth-be-told, the update on the DataGrid control is pretty un-friendly, since if you have more than a few fields, it becomes be wider than any user's screen. So we are pretty much stuck making two pages for each object: a list/delete page with a GridView, and an create/update page with a DetailsView.
Wouldn't it be nice to halve your work by only having one page per object? That sort of thinking led me to create a ComboGrid control which, as the name implies, consists principally of a GridView, but switches to a DetailsView on edit and insert. This sweet little guy has saved me a bunch of work, and reduced the amount of web pages I've had to create. So I figured I would post the code, so that it can help other people as well. If it isn't directly helpful, then perhaps at least it will be a good starting point for others, or a useful insight into how these controls work behind the scenes.
A few notes:
- The ComboMaster is the primary control
- It instantiates the ComboGrid and ComboView and necessary
- It is bindable in the same ways as either of the controls it represents (DataSource, DataSourceId)
- It uses the Columns collection for its properties, as opposed to the Fields collection
- It only supports TemplateFields for the moment. (Why? Because I haven't gotten around to any others yet)
- I've been using this one for a bit, so it is pretty stable, but as usual, this is intended primarily for example purposes, and comes with no warranty
Download the Sample Code!
- Brent
Attachment(s): ComboMaster.zip