Nested Data Web Controls (VB)
Download Sample App or Download PDF
In this tutorial we will explore how to use a Repeater nested inside another Repeater. The examples will illustrate how to populate the inner Repeater both declaratively and programmatically.
Introduction
In addition to static HTML and databinding syntax, templates can also include Web controls and User Controls. These Web controls can have their properties assigned via declarative, databinding syntax, or can be accessed programmatically in the appropriate server-side event handlers.
By embedding controls within a template, the appearance and user experience can be customized and improved upon. For example, in the Using TemplateFields in the GridView Control tutorial, we saw how to customize the GridView s display by adding a Calendar control in a TemplateField to show an employee s hire date; in the Adding Validation Controls to the Editing and Inserting Interfaces and Customizing the Data Modification Interface tutorials, we saw how to customize the editing and inserting interfaces by adding validation controls, TextBoxes, DropDownLists, and other Web controls.
Templates can also contain other data Web controls. That is, we can have a DataList that contains another DataList (or Repeater or GridView or DetailsView, and so on) within its templates. The challenge with such an interface is binding the appropriate data to the inner data Web control. There are a few different approaches available, ranging from declarative options using the ObjectDataSource to programmatic ones.
In this tutorial we will explore how to use a Repeater nested inside another Repeater. The outer Repeater will contain an item for each category in the database, displaying the category s name and description. Each category item s inner Repeater will display information for each product belonging to that category (see Figure 1) in a bulleted list. Our examples will illustrate how to populate the inner Repeater both declaratively and programmatically.
Figure 1: Each Category, Along with its Products, are Listed (Click to view full-size image)
Step 1: Creating the Category Listing
When building a page that uses nested data Web controls, I find it helpful to design, create, and test the outermost data Web control first, without even worrying about the inner nested control. Therefore, let s start by walking through the steps necessary to add a Repeater to the page that lists the name and description for each category.
Start by opening the NestedControls.aspx
page in the DataListRepeaterBasics
folder and add a Repeater control to the page, setting its ID
property to CategoryList
. From the Repeater s smart tag, choose to create a new ObjectDataSource named CategoriesDataSource
.
Figure 2: Name the New ObjectDataSource CategoriesDataSource
(Click to view full-size image)
Configure the ObjectDataSource so that it pulls its data from the CategoriesBLL
class s GetCategories
method.
Figure 3: Configure the ObjectDataSource to Use the CategoriesBLL
Class s GetCategories
Method (Click to view full-size image)
To specify the Repeater s template content we need to go to the Source view and manually enter the declarative syntax. Add an ItemTemplate
that displays the category s name in an <h4>
element and the category s description in a paragraph element (<p>
). Furthermore, let s separate each category with a horizontal rule (<hr>
). After making these changes your page should contain declarative syntax for the Repeater and ObjectDataSource that is similar to the following:
[!code-aspxMain]
1: <asp:Repeater ID="CategoryList" DataSourceID="CategoriesDataSource"
2: EnableViewState="False" runat="server">
3: <ItemTemplate>
4: <h4><%# Eval("CategoryName") %></h4>
5: <p><%# Eval("Description") %></p>
6: </ItemTemplate>
7: <SeparatorTemplate>
8: <hr />
9: </SeparatorTemplate>
10: </asp:Repeater>
11: <asp:ObjectDataSource ID="CategoriesDataSource" runat="server"
12: OldValuesParameterFormatString="original_{0}"
13: SelectMethod="GetCategories" TypeName="CategoriesBLL">
14: </asp:ObjectDataSource>
Figure 4 shows our progress when viewed through a browser.
Figure 4: Each Category s Name and Description is Listed, Separated by a Horizontal Rule (Click to view full-size image)
Step 2: Adding the Nested Product Repeater
With the category listing complete, our next task is to add a Repeater to the CategoryList
s ItemTemplate
that displays information about those products belonging to the appropriate category. There are a number of ways we can retrieve the data for this inner Repeater, two of which we???ll explore shortly. For now, let s just create the products Repeater within the CategoryList
Repeater s ItemTemplate
. Specifically, let s have the product Repeater display each product in a bulleted list with each list item including the product s name and price.
To create this Repeater we need to manually enter the inner Repeater s declarative syntax and templates into the CategoryList
s ItemTemplate
. Add the following markup within the CategoryList
Repeater s ItemTemplate
:
[!code-aspxMain]
1: <asp:Repeater ID="ProductsByCategoryList" EnableViewState="False"
2: runat="server">
3: <HeaderTemplate>
4: <ul>
5: </HeaderTemplate>
6: <ItemTemplate>
7: <li><strong><%# Eval("ProductName") %></strong>
8: (<%# Eval("UnitPrice", "{0:C}") %>)</li>
9: </ItemTemplate>
10: <FooterTemplate>
11: </ul>
12: </FooterTemplate>
13: </asp:Repeater>
Step 3: Binding the Category-Specific Products to the ProductsByCategoryList Repeater
If you visit the page through a browser at this point, your screen will look the same as in Figure 4 because we ve yet to bind any data to the Repeater. There are a few ways that we can grab the appropriate product records and bind them to the Repeater, some more efficient than others. The main challenge here is getting back the appropriate products for the specified category.
The data to bind to the inner Repeater control can either be accessed declaratively, through an ObjectDataSource in the CategoryList
Repeater s ItemTemplate
, or programmatically, from the ASP.NET page s code-behind page. Similarly, this data can be bound to the inner Repeater either declaratively - through the inner Repeater s DataSourceID
property or through declarative databinding syntax or programmatically by referencing the inner Repeater in the CategoryList
Repeater s ItemDataBound
event handler, programmatically setting its DataSource
property, and calling its DataBind()
method. Let s explore each of these approaches.
Accessing the Data Declaratively with an ObjectDataSource Control and theItemDataBound
Event Handler
Since we ve used the ObjectDataSource extensively throughout this tutorial series, the most natural choice for accessing data for this example is to stick with the ObjectDataSource. The ProductsBLL
class has a GetProductsByCategoryID(categoryID)
method that returns information about those products that belong to the specified categoryID
. Therefore, we can add an ObjectDataSource to the CategoryList
Repeater s ItemTemplate
and configure it to access its data from this class s method.
Unfortunately, the Repeater doesn t allow its templates to be edited through the Design view so we need to add the declarative syntax for this ObjectDataSource control by hand. The following syntax shows the CategoryList
Repeater s ItemTemplate
after adding this new ObjectDataSource (ProductsByCategoryDataSource
):
[!code-aspxMain]
1: <h4><%# Eval("CategoryName") %></h4>
2: <p><%# Eval("Description") %></p>
3: <asp:Repeater ID="ProductsByCategoryList" EnableViewState="False"
4: DataSourceID="ProductsByCategoryDataSource" runat="server">
5: <HeaderTemplate>
6: <ul>
7: </HeaderTemplate>
8: <ItemTemplate>
9: <li><strong><%# Eval("ProductName") %></strong> -
10: sold as <%# Eval("QuantityPerUnit") %> at
11: <%# Eval("UnitPrice", "{0:C}") %></li>
12: </ItemTemplate>
13: <FooterTemplate>
14: </ul>
15: </FooterTemplate>
16: </asp:Repeater>
17: <asp:ObjectDataSource ID="ProductsByCategoryDataSource" runat="server"
18: SelectMethod="GetProductsByCategoryID" TypeName="ProductsBLL">
19: <SelectParameters>
20: <asp:Parameter Name="CategoryID" Type="Int32" />
21: </SelectParameters>
22: </asp:ObjectDataSource>
When using the ObjectDataSource approach we need to set the ProductsByCategoryList
Repeater s DataSourceID
property to the ID
of the ObjectDataSource (ProductsByCategoryDataSource
). Also, notice that our ObjectDataSource has an <asp:Parameter>
element that specifies the categoryID
value that will be passed into the GetProductsByCategoryID(categoryID)
method. But how do we specify this value? Ideally, we d be able to just set the DefaultValue
property of the <asp:Parameter>
element using databinding syntax, like so:
[!code-aspxMain]
1: <asp:Parameter Name="CategoryID" Type="Int32"
2: DefaultValue='<%# Eval("CategoryID")' />
Unfortunately, databinding syntax is only valid in controls that have a DataBinding
event. The Parameter
class lacks such an event and therefore the above syntax is illegal and will result in a runtime error.
To set this value, we need to create an event handler for the CategoryList
Repeater s ItemDataBound
event. Recall that the ItemDataBound
event fires once for each item bound to the Repeater. Therefore, each time this event fires for the outer Repeater we can assign the current CategoryID
value to the ProductsByCategoryDataSource
ObjectDataSource s CategoryID
parameter.
Create an event handler for the CategoryList
Repeater s ItemDataBound
event with the following code:
[!code-vbMain]
1: Protected Sub CategoryList_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) _
2: Handles CategoryList.ItemDataBound
3: If e.Item.ItemType = ListItemType.AlternatingItem _
4: OrElse e.Item.ItemType = ListItemType.Item Then
5: ' Reference the CategoriesRow object being bound to this RepeaterItem
6: Dim category As Northwind.CategoriesRow = _
7: CType(CType(e.Item.DataItem, System.Data.DataRowView).Row, _
8: Northwind.CategoriesRow)
9: ' Reference the ProductsByCategoryDataSource ObjectDataSource
10: Dim ProductsByCategoryDataSource As ObjectDataSource = _
11: CType(e.Item.FindControl("ProductsByCategoryDataSource"), _
12: ObjectDataSource)
13: ' Set the CategoryID Parameter value
14: ProductsByCategoryDataSource.SelectParameters("CategoryID").DefaultValue = _
15: category.CategoryID.ToString()
16: End If
17: End Sub
This event handler starts by ensuring that we re dealing with a data item rather than the header, footer, or separator item. Next, we reference the actual CategoriesRow
instance that has just been bound to the current RepeaterItem
. Finally, we reference the ObjectDataSource in the ItemTemplate
and assign its CategoryID
parameter value to the CategoryID
of the current RepeaterItem
.
With this event handler, the ProductsByCategoryList
Repeater in each RepeaterItem
is bound to those products in the RepeaterItem
s category. Figure 5 shows a screen shot of the resulting output.
Figure 5: The Outer Repeater Lists Each Category; the Inner One Lists the Products for that Category (Click to view full-size image)
Accessing the Products by Category Data Programmatically
Instead of using an ObjectDataSource to retrieve the products for the current category, we could create a method in our ASP.NET page s code-behind class (or in the App_Code
folder or in a separate Class Library project) that returns the appropriate set of products when passed in a CategoryID
. Imagine that we had such a method in our ASP.NET page s code-behind class and that it was named GetProductsInCategory(categoryID)
. With this method in place we could bind the products for the current category to the inner Repeater using the following declarative syntax:
[!code-aspxMain]
1: <asp:Repeater runat="server" ID="ProductsByCategoryList" EnableViewState="False"
2: DataSource='<%# GetProductsInCategory(CType(Eval("CategoryID"), Integer)) %>'>
3: ...
4: </asp:Repeater>
The Repeater s DataSource
property uses the databinding syntax to indicate that its data comes from the GetProductsInCategory(categoryID)
method. Since Eval("CategoryID")
returns a value of type Object
, we cast the object to an Integer
before passing it into the GetProductsInCategory(categoryID)
method. Note that the CategoryID
accessed here via the databinding syntax is the CategoryID
in the outer Repeater (CategoryList
), the one that s bound to the records in the Categories
table. Therefore, we know that CategoryID
cannot be a database NULL
value, which is why we can blindly cast the Eval
method without checking if we re dealing with a DBNull
.
With this approach, we need to create the GetProductsInCategory(categoryID)
method and have it retrieve the appropriate set of products given the supplied categoryID
. We can do this by simply returning the ProductsDataTable
returned by the ProductsBLL
class s GetProductsByCategoryID(categoryID)
method. Let s create the GetProductsInCategory(categoryID)
method in the code-behind class for our NestedControls.aspx
page. Do so using the following code:
[!code-vbMain]
1: Protected Function GetProductsInCategory(ByVal categoryID As Integer) _
2: As Northwind.ProductsDataTable
3: ' Create an instance of the ProductsBLL class
4: Dim productAPI As ProductsBLL = New ProductsBLL()
5: ' Return the products in the category
6: Return productAPI.GetProductsByCategoryID(categoryID)
7: End Function
This method simply creates an instance of the ProductsBLL
method and returns the results of the GetProductsByCategoryID(categoryID)
method. Note that the method must be marked Public
or Protected
; if the method is marked Private
, it will not be accessible from the ASP.NET page s declarative markup.
After making these changes to use this new technique, take a moment to view the page through a browser. The output should be identical to the output when using the ObjectDataSource and ItemDataBound
event handler approach (refer back to Figure 5 to see a screen shot).
[!NOTE] It may seem like busywork to create the
GetProductsInCategory(categoryID)
method in the ASP.NET page s code-behind class. After all, this method simply creates an instance of theProductsBLL
class and returns the results of itsGetProductsByCategoryID(categoryID)
method. Why not just call this method directly from the databinding syntax in the inner Repeater, like:DataSource='<%# ProductsBLL.GetProductsByCategoryID(CType(Eval("CategoryID"), Integer)) %>'
? Although this syntax won t work with our current implementation of theProductsBLL
class (since theGetProductsByCategoryID(categoryID)
method is an instance method), you could modifyProductsBLL
to include a staticGetProductsByCategoryID(categoryID)
method or have the class include a staticInstance()
method to return a new instance of theProductsBLL
class.
While such modifications would eliminate the need for the GetProductsInCategory(categoryID)
method in the ASP.NET page s code-behind class, the code-behind class method gives us more flexibility in working with the data retrieved, as we???ll see shortly.
Retrieving All of the Product Information at Once
The two pervious techniques we ve examined grab those products for the current category by making a call to the ProductsBLL
class s GetProductsByCategoryID(categoryID)
method (the first approach did so through an ObjectDataSource, the second through the GetProductsInCategory(categoryID)
method in the code-behind class). Each time this method is invoked, the Business Logic Layer calls down to the Data Access Layer, which queries the database with a SQL statement that returns rows from the Products
table whose CategoryID
field matches the supplied input parameter.
Given N categories in the system, this approach nets N + 1 calls to the database one database query to get all of the categories and then N calls to get the products specific to each category. We can, however, retrieve all the needed data in just two database calls one call to get all of the categories and another to get all of the products. Once we have all of the products, we can filter those products so that only the products matching the current CategoryID
are bound to that category s inner Repeater.
To provide this functionality, we only need to make a slight modification to the GetProductsInCategory(categoryID)
method in our ASP.NET page s code-behind class. Rather than blindly returning the results of the ProductsBLL
class s GetProductsByCategoryID(categoryID)
method, we can instead first access all of the products (if they haven t been accessed already) and then return just the filtered view of the products based on the passed-in CategoryID
.
[!code-vbMain]
1: Private allProducts As Northwind.ProductsDataTable = Nothing
2: Protected Function GetProductsInCategory(ByVal categoryID As Integer) _
3: As Northwind.ProductsDataTable
4: ' First, see if we've yet to have accessed all of the product information
5: If allProducts Is Nothing Then
6: Dim productAPI As ProductsBLL = New ProductsBLL()
7: allProducts = productAPI.GetProducts()
8: End If
9: ' Return the filtered view
10: allProducts.DefaultView.RowFilter = "CategoryID = " & categoryID
11: Return allProducts
12: End Function
Note the addition of the page-level variable, allProducts
. This holds information about all of the products and is populated the first time the GetProductsInCategory(categoryID)
method is invoked. After ensuring that the allProducts
object has been created and populated, the method filters the DataTable s results such that only those rows whose CategoryID
matches the specified CategoryID
are accessible. This approach reduces the number of times the database is accessed from N + 1 down to two.
This enhancement does not introduce any change to the rendered markup of the page, nor does it bring back fewer records than the other approach. It simply reduces the number of calls to the database.
[!NOTE] One might intuitively reason that reducing the number of database accesses would assuredly improve performance. However, this might not be the case. If you have a large number of products whose
CategoryID
isNULL
, for example, then the call to theGetProducts
method returns a number of products that are never displayed. Moreover, returning all of the products can be wasteful if you re only showing a subset of the categories, which might be the case if you have implemented paging.
As always, when it comes to analyzing the performance of two techniques, the only surefire measure is to run controlled tests tailored for your application s common case scenarios.
Summary
In this tutorial we saw how to nest one data Web control within another, specifically examining how to have an outer Repeater display an item for each category with an inner Repeater listing the products for each category in a bulleted list. The main challenge in building a nested user interface lies in accessing and binding the correct data to the inner data Web control. There are a variety of techniques available, two of which we examined in this tutorial. The first approach examined used an ObjectDataSource in the outer data Web control s ItemTemplate
that was bound to the inner data Web control through its DataSourceID
property. The second technique accessed the data via a method in the ASP.NET page s code-behind class. This method can then be bound to the inner data Web control s DataSource
property through databinding syntax.
While the nested user interface examined in this tutorial used a Repeater nested within a Repeater, these techniques can be extended to the other data Web controls. You can nest a Repeater within a GridView, or a GridView within a DataList, and so on.
Happy Programming!
About the Author
Scott Mitchell, author of seven ASP/ASP.NET books and founder of 4GuysFromRolla.com, has been working with Microsoft Web technologies since 1998. Scott works as an independent consultant, trainer, and writer. His latest book is Sams Teach Yourself ASP.NET 2.0 in 24 Hours. He can be reached at mitchell@4GuysFromRolla.com. or via his blog, which can be found at http://ScottOnWriting.NET.
Special Thanks To
This tutorial series was reviewed by many helpful reviewers. Lead reviewers for this tutorial were Zack Jones and Liz Shulok. Interested in reviewing my upcoming MSDN articles? If so, drop me a line at mitchell@4GuysFromRolla.com.
|