In this tutorial, we will demostrate how to use nested Repeater control to display hierarchical data using ASP.NET 2.0 and VB.NET. You can apply this usage to the other data binding controls as well. For instance, to let DataGrid nest DataGrid, DataList nest DataList etc.
The Repeater Web server control is a container control that allows you to create custom lists out of any data that is available to the page. The Repeater control does not have a built-in rendering of its own, which means that you must provide the layout for the Repeater control by creating templates. When the page runs, the Repeater control loops through the records in the data source and renders an item for each record.
First, import the System.Data.SqlClient namespace. The System.Data.SqlClient namespace is the.NET Framework Data Provider for SQL Server. The.NET Framework Data Provider for SQL Server describes a collection of classes used to access a SQL Server database in the managed space.
| Imports System.Data.SqlClient |
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
Then, add a new web form to the solution, name it Nestedrepeater.aspx, and create a connection to the sample database pubs. Binding the table of Authors to Repeater Control.
|
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Create the connection and DataAdapter for the Authors table.
Dim cnn As New SqlConnection("server=(local);database=pubs;uid=sa;pwd=;")
Dim cmd1 As New SqlDataAdapter("select * from authors", cnn)
'Create and fill the DataSet.
Dim ds As New DataSet()
cmd1.Fill(ds, "authors")
'Bind the Authors table to the parent Repeater control, and call DataBind.
parent1.DataSource = ds.Tables("authors")
Page.DataBind()
cnn.Close()
End Sub
|
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
In the Page_Load, add the child table data binding, and create relationship between the tables of author and title.
'Create a second DataAdapter for the Titles table.
Dim cmd2 As New SqlDataAdapter("select * from titles,titleauthor,authors where titles.title_id=titleauthor.title_id and authors.au_id=titleauthor.au_id", cnn)
cmd2.Fill(ds, "titles")
'Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("myrelation", ds.Tables("authors").Columns("au_id"), ds.Tables("titles").Columns("au_id"))
|
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
The front Nestedrepeater .aspx page looks something like this:
<body>
<form id="form1" runat="server">
<fieldset>
<legend>Nestedrepeater</legend>
<table>
<tr>
<td><b>Show the author and his works from the databases of pubs</b></td></tr>
<tr>
<td align="center">
<asp:repeater id="parent" runat="server">
<itemtemplate>
<b><%# DataBinder.Eval(Container.DataItem,"au_lname") %></b><br>
<asp:repeater id="child" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' runat="server">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "[\"title\"]")%><br>
</itemtemplate>
</asp:repeater>
</itemtemplate>
</asp:repeater></td></tr>
</table></fieldset>
</form>
</body>
|
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
The flow for the code behind page is as follows.
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Partial Class Nestedrepeater
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Create the connection and DataAdapter for the Authors table.
Dim cnn As New SqlConnection("server=(local);database=pubs;uid=sa;pwd=;")
Dim cmd1 As New SqlDataAdapter("select * from authors", cnn)
'Create and fill the DataSet.
Dim ds As New DataSet()
cmd1.Fill(ds, "authors")
'Create a second DataAdapter for the Titles table.
Dim cmd2 As New SqlDataAdapter("select * from titles,titleauthor,authors where titles.title_id=titleauthor.title_id and authors.au_id=titleauthor.au_id", cnn)
cmd2.Fill(ds, "titles")
'Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("myrelation", ds.Tables("authors").Columns("au_id"), ds.Tables("titles").Columns("au_id"))
'Bind the Authors table to the parent Repeater control, and call DataBind.
parent1.DataSource = ds.Tables("authors")
Page.DataBind()
cnn.Close()
End Sub
End Class
|
Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!