Daniyal Javaid Daniyal Javaid - 3 months ago 38
ASP.NET (C#) Question

Nested Repeater Using DataTable Asp.net

The problem is when I use dt1 as rpt2.DataSource(nested repeater source) the program runs fine , but as I use dt2 as source(and make Eval() changes accordingly) the outputs are of only First Repeater.

Output for dt2=>

1 Rams

2 Processors

3 GraphicCards

Output when i make changes for dt1=>

1 Rams

1

2

3

2 Processors

1

2

3

3 GraphicCards

1

2

3

<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="my_repeater1" runat="server" OnItemDataBound="my_repeater1_ItemDataBound">
<ItemTemplate>
<asp:Label Text='<%#Eval("ID")%>' ID="my_text1" runat="server" />
<asp:Label Text='<%#Eval("CATEGORY")%>' ID="my_text2" runat="server" />
<br />
<asp:Repeater runat="server" ID="my_repeater2">
<ItemTemplate>
<asp:Label Text='<%#Eval("ITEMS")%>' ID="lbl1" runat="server" />
<br />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</div>
</form>




public partial class WebForm1 : System.Web.UI.Page
{
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
DataTable dt3 = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{

dt1.Columns.Add("ID");
dt1.Columns.Add("CATEGORY");
dt1.Rows.Add("1", "Rams");
dt1.Rows.Add("2", "Processors");
dt1.Rows.Add("3", "GraphicCards");
my_repeater1.DataSource = dt1;
my_repeater1.DataBind();

dt2.Columns.Add("ID");
dt2.Columns.Add("ITEMS");
dt2.Rows.Add("1", "DDR1");
dt2.Rows.Add("2", "DDR2");
dt2.Rows.Add("3", "DDR3");

}

protected void my_repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Repeater rpt2 = (Repeater)e.Item.FindControl("my_repeater2");
rpt2.DataSource = dt2;
rpt2.DataBind();

}
}

Answer

When my_repeater1.DataBind() is executed .NET will start building my_repeater1. For every row added to my_repeater1, my_repeater1_ItemDataBound is called. And since you use dt2 as source for the nested Repeater you will get no data since dt2 not yet populated with data. That only happens in your example after the building of my_repeater1 is complete.

Comments