Build a Nested GridView Control with ASP.NET Portland OR

Take the tedium out of building complex GUIs in ASP.NET by creating nested GridView controls.

Local Companies

Cascade Custom Software
503-922-0135
1000 SW Broadway
Portland, OR
Advantyx Software LLC
503-246-9299
6501 SW Macadam Ave
Portland, OR
Aravo Solutions
503-224-4049
2627 NW Nicolai St
Portland, OR
Axis Clinical Software Inc
503-292-3022
6443 SW Beaverton Hillsdale Hwy
Portland, OR
Cendix
503.789.2676
501 4th Street, Suite 741
Lake Oswego, OR
Artisan Software Tools Inc
503-245-6200
10220 SW Greenburg Rd
Portland, OR
Vision33
971-255-0162
The Lincoln Center, 10260 SW Greenburg Road, 4th Floor
Portland, OR
Paradigma Software
(503) 574-2776
6107 SW Murray Blvd #151
Beaverton, OR
ClearStar.net
360-892-0687
915 Broadway
Vancouver, WA
BCSI Solutions
503-641-5584
15242 NW GreenBrier Parkway
Beaverton, OR

provided by: 
Originally published at Internet.com


The biggest oversight in ASP.NET is the lack of an easy way to create fully functional nested grid and user control views (nested means one or more controls exist in one or more GridView cells; these controls may be grids or user control grids nested n-levels deep.) Propagating data downstream, permitting full editing functionality, and event handling are still very labor intensive. This is a glaring deficiency in the way you can develop Web applications with ASP.NET.

Suppose you want to show customers, orders, order details, and maybe product information from a Northwind database in a related way. The mechanically easy way to derive such a presentation is to establish links between pages and have each subsequent page zoom in to a related downstream piece of information. To put it mildly, this type of implementation results in a tedious series of clicking, posting, and page navigation. (Even with the asynchronous capabilities of Atlas, all of the required clicking can be cumbersome.) Worse still, editing can be a nightmare with partial state changes and database modifications.

This article demonstrates how to take the first step towards making n-dimensionally complex ASP.NET presentations, vastly reducing the number of round trips, and greatly improving data management. You will see how to create complex views easily. (In reality, though, this is something Microsoft should address. They easily could use the implementation style in this article to take the tedium out of building complex GUIs in ASP.NET.)

The objective is simple: Show all of the orders for each customer in a single page (see Figure 1). (The demonstration is not interested in "prettification"; with a style sheet, you could easily dress up the view.)



Click here for a larger image.

Figure 1: A Nested GridView Control Showing Customers and Orders

Building a Master Detail View Using the GridView

Due to space constraints, this article shows only two levels of nesting. However, by the time you are done, you will see that this approach works for n levels of nesting. The only caution is that with very complex presentation styles bandwidth can become an issue. Tip: If you want a nicely color-coordinated Web application, try Behr's Smart Color tool. It will help you pick a perfectly coordinated, tri-chromatic color palette. Tell them I sent you.

Reviewing the Constituent Parts of the Page

Each level of nesting is composed of three basic parts: * A page or user control, which basically are the same thing for this demo's purposes * A GridView (or some other similar type control) * A UserControl, which will be used to contain the nested elements * The nested controls to implement the UserControl (these can be individual controls or another GridView)

At the highest level of the nested view is an ASP.NET Web Page. The page is important not only because it will be the root of the presentation layer, but it also is where user data will be stored in session and where changes will be persisted.

The page-level GridView simply repeats and displays the data at the highest level of the relationship. (It is ultimately rendered as an HTML table.)

At every subsequent level of nesting, you will use a UserControl-an important element that enables you to divide up pieces of the presentation into manageable parts. The UserControl essentially provides a blank canvas that you can use to design a sub-view and implement the code-behind for that sub-view.

Finally, any child controls you'd like to nest are painted on the UserControls. For now, ignore the mechanics (that is, the code-behind) and focus on building the view.

Assembling the Pieces of the View

The best way to walk through the presentation is to list each step with a brief explanation. Even if you are unfamiliar with GridView, smart tags, or UserControls, the detailed steps should walk you through the process: 1. Create a new Web page (or use an existing one in your project). 2. Add a GridView to that page. If the GridView is part of a whole page, explore Master pages as a way to get consistent looking pages. DataList and Repeater controls work here too. 3. Define bound columns for every element of data in your master object. (I use custom objects more often than not because I like the greater control afforded by custom controls, and I don't need to drag ADO.NET into my presentation layer.) 4. Convert one or more bound columns into a template column by clicking on the smart tag at the top-left corner of the GridView, selecting Edit columns from the smart menu, selecting the field to convert, and clicking the self-explanatory link for converting a bound column to a template column (see Figure 2).



Click here for a larger image.

Figure 2: Click "Convert this field into a TemplateField"

(In the example in Figure 1, the last column is the template column.) 5. Close the Fields dialog. 6. Add a UserControl to your project. 7. Click the Smart tag on the GridView again and select Edit Templates. 8. Drag and drop the UserControl from the solution explorer to the ItemTemplate, remove the existing Label and TextBox, and click Edit UserControl from the Smart tag menu (see Figure 3).



Click here for a larger image.

Figure 3: Begin Designing the Nested Sub-view 9. Add a second GridView to the UserControl and you have the basic design.

Listing 1 shows you approximately how the ASP code will look after you have completed these steps.

Listing 1: ASP Code After You Have Completed Steps 1 Through 9 <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <%@ Register src="http://www.developer.com/Orders.ascx" TagName="Orders" TagPrefix="uc2"%> <%@ Register src="http://www.developer.com/OrdersControl.ascx" TagName="OrdersControl" TagPrefix="uc1" %> Untitled Page

Featured Local Company

Cascade Custom Software

503-922-0135
1000 SW Broadway
Portland, OR

Related Local Events
WOOD TECHNOLOGY CLINIC & SHOW 2010
Dates: 3/9/2010 - 3/11/2010
Location: Oregon Convention Center
Portland, OR
View Details

Party in the Pinot
Dates: 7/25/2009 - 7/25/2009
Location: Oswego Hills Winery
West Linn, OR
View Details

WebVisions 2009
Dates: 5/20/2009 - 5/22/2009
Location: Oregon Convention Center
Portland, OR
View Details

Technology and IP Networking Event
Dates: 5/19/2009 - 5/19/2009
Location: Oregon Zoo
Portland, OR
View Details

Fall Knit & Crochet Show
Dates: 5/14/2009 - 5/17/2009
Location: Doubletree Hotel Portland, Lloyd Center
Portland, OR
View Details