A common scenario in a repeating data display would be like this:
Category Name Product 1 Product 2 Product 3
The data would repeat, listing each category name, and then, under that category, each product in that particular category. In Classic ASP, there were several ways of doing this, of which, arguably the most common approach is/was a For/Next Loop with a Recordset.
Now, with the advent of ASP.Net, we can acutually Nest repeating controls (DataList, Repeater) to do this. I’ll admit, even if you’ve had a little relational database experience, this will, at first be a little daunting – but once you fully understand it, it seems extremely logical.
In this tutorial, we’re going to use a DataList control, the SQL Managed Provider, and the Northwind Database (Categories and Products tables). we could have just as easily used a Repeater control, with different formatting. Here are the steps to produce this scenario:
- In the HTML:
- Create/instantiate one DataList control (ID=dlCategories), with an ItemTemplate (Container.DataItem=CategoryName)
- Inside that ItemTemplate, create/instantiate another DataList control, with an ItemTemplate(Container.DataItem=ProductName)
The difference here is that we’re going to add a DataSource (datasource=’<%# Container.DataItem.Row.GetChildRows(“myrelation”) %>’) – Don’t worry about what this means – it’s in the code section
Continues…