Adding the Database

To demonstrate how to nest repeaters, we will need to create a simple web site which will contain a sample database and a web form. For this example, we will be using the PUBS SQL sample database that will be included with the source of the project. To begin, create a new ASP.NET Empty Web Site and:

  1. Right click the project in your solution explorer.
  2. Select Add ASP.NET Folder.
  3. Select App_Data.
  4. Right click the App_Data folder.
  5. Select add existing item…
  6. Select the PUBS.MDF database.
  7. Click add.

Next, we need to add a connection string that will allow us to connect to the database. To do this, open up the Web.Config file for editing and add the following code:

This will simply direct our web site to the database.

Creating the Form

Next, we need to create a simple form that we will add our repeater to. To do this:

  1. Right click the project in your solution explorer.
  2. Select add new item…
  3. Select a web form.
  4. Name it ‘Default.aspx’.
  5. Click add.
  6. Open Default.aspx up to source mode.
  7. Drag and drop a repeater onto the web form.
  8. Change the ID property to ‘parent’.
  9. Add the following to the repeater’s item template:
  10. Drag and drop a repeater into the item template.
  11. Change the ID property to ‘child’.
  12. Set the DataSource property to ‘<%# ((DataRowView)Container.DataItem).Row.GetChildRows(“myrelation”) %>’.
  13. Add the following code to the repeater’s item template:

Next, we need to add the code that will select the data from our database and bind it to our repeaters. To do this, open Default.aspx.vb for editing and add the following code to the Page_Load event method:

Testing
To test this out load up the web site. Ensure that our repeaters display a list of authors followed by the list of their works. This demonstrates how you can easily nest repeaters within each other to display nested lists.

Download Source Files