Nested repeater databinding question

E

Ed Allan

http://ejaconsulting.com/nestedrepeater/NestedRepeater.txt
-----Original Message-----
Doh! The HTML has all been rendered . . .

Right click on this link and select 'Save target as ..'
to get the code in a text file.

Thanks - Ed
-----Original Message-----
I have created an ASP.Net page - see code below - adapted
from http://support.microsoft.com/default.aspx? scid=kb;EN-
US;306154 which contains 3 repeaters nested inside of
each other.

When it executes it throws an InvalidCastException when
getting the datasource of the grandchild repeater.
However this datasource is fine for the child repeater -
remove the grandchild repeater from the page and it runs
as expected.

Can anyone tell me what is wrong with the datasource of
the grandchild repeater? What should it be?

Thanks - Ed


HTML file:
==========
<code>

<%@ Page language="c#"
Codebehind="NestedRepeater.aspx.cs"
AutoEventWireup="false"
Inherits="NestedRepeater.NestedRepeater" %>
<%@ Import Namespace="System.Data" %>

<html>
<head>
<title>Nested Repeater Example</title>
</head>
<body>
<form runat="server" id="Form1">

<!-- start parent repeater -->
<asp:repeater id="parentRepeater" runat="server">
<itemtemplate>
<b>Company : <%# DataBinder.Eval
(Container.DataItem,"CompanyName") %></b><br/>

<!-- start child repeater -->
<asp:repeater id="childRepeater" datasource='<%#
((DataRowView)Container.DataItem).Row.GetChildRows
("Customers_Orders") %>' runat="server">

<itemtemplate>
- Order Date : <%# DataBinder.Eval
(Container.DataItem, "[\"OrderDate\"]")%><br/>

<!-- start grandchild repeater -->
<asp:repeater id="grandChildRepeater"
datasource='<%# ((DataRowView)
Container.DataItem).Row.GetChildRows
("Orders_OrderDetails") %>' runat="server">
<itemtemplate>
- - Product ID : <%#
DataBinder.Eval(Container.DataItem, "[\"ProductID\"]")%
</itemtemplate>
</asp:repeater>
<!-- end grandchild repeater -->

</itemtemplate>
</asp:repeater>
<!-- end child repeater -->

</itemtemplate>
</asp:repeater>
<!-- end parent repeater -->

</form>
</body>
</html>

</code>

Code behind file:
=================
<code>

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NestedRepeater
{
public class NestedRepeater : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.Repeater parentRepeater;

public NestedRepeater()
{
Page.Init += new
System.EventHandler(Page_Init);
}

public void Page_Load(object sender,
EventArgs e)
{
//Create the connection and
DataAdapter for the Customers (parent) table.
SqlConnection cnn = new
SqlConnection("Data Source=(local);Integrated
Security=SSPI;Trusted_Connection=yes;Initial
Catalog=Northwind;");
SqlDataAdapter daCustomers = new
SqlDataAdapter("select CustomerID, CompanyName from
Customers order by CompanyName", cnn);

//Create a second DataAdapter for
the Orders (child) table.
SqlDataAdapter daOrders = new
SqlDataAdapter("select OrderID, CustomerID, OrderDate
from Orders order by OrderDate", cnn);

//Create a third DataAdapter for
the OrderDetails (grandchild) table.
SqlDataAdapter daOrderDetails =
new SqlDataAdapter("select OrderID, ProductID from [Order
Details] order by OrderID", cnn);

//Create and fill the DataSet.
DataSet ds = new DataSet();
DataTable dtCustomers =
ds.Tables.Add("Customers");
DataTable dtOrders = ds.Tables.Add
("Orders");
DataTable dtOrderDetails =
ds.Tables.Add("OrderDetails");
daCustomers.Fill(dtCustomers);
daOrders.Fill(dtOrders);
daOrderDetails.Fill
(dtOrderDetails);

//Create the relation between the
Customers and Orders tables.
ds.Relations.Add
("Customers_Orders", ds.Tables["Customers"].Columns
["CustomerID"], ds.Tables["Orders"].Columns
["CustomerID"]);

//Create the relation between the
Orders and OrderDetails tables.
ds.Relations.Add
("Orders_OrderDetails", ds.Tables["Orders"].Columns
["OrderID"], ds.Tables["OrderDetails"].Columns
["OrderID"]);

//Bind the Authors table to the
parent Repeater control, and call DataBind.
parentRepeater.DataSource =
ds.Tables["Customers"];
Page.DataBind();

//Close the connection.
cnn.Close();
}

private void Page_Init(object sender,
EventArgs e)
{
InitializeComponent();
}

private void InitializeComponent()
{
this.Load += new
System.EventHandler(this.Page_Load);
}
}
}

</code>
.
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Nested Repeater controls 0
Nested repeater with 3 levels 4
Nested Repeater within Grid 0
nested repeaters 0
Nested Repeater 0
Nested Repeater Controls 0
repeater question 1
repeater inside a repeater problem. 0

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top