

Navigator : Home > Tutorials > Controls Tutorials > ...
To display hierarchical data using ASP.NET 2.0 (VB.NET)
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 |
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
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 |
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")) |
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 |
Looking for the C#.NET 2005 Version? Click Here!
Try Server Intellect for Windows Server Hosting. Quality and Quantity!