Click here to Skip to main content
14,878,819 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


I have done nested repeater Control.but the output what i want am unable to get that.
Kindly help me out.


Here Is My Complete Code:

Source Code:

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReptrinsidReptr.aspx.cs" Inherits="Mystuff.Controls.ReptrinsidReptr" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
    <HeaderTemplate>
        <div style="background:Yellow">
<asp:Label ID="Label1" runat="server" Text="Name" Width="32%" ForeColor="Fuchsia"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Designation" Width="32%" ForeColor="Fuchsia"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="Location" Width="32%" ForeColor="Fuchsia"></asp:Label>
        </div>
    </HeaderTemplate>
    <ItemTemplate>
   <div style="background:Yellow">
<asp:TextBox ID="Label4" runat="server" Text='<%#Eval("Name") %>' Width="32%" BackColor="Cyan"></asp:TextBox>
<asp:TextBox ID="Label5" runat="server" Text='<%#Eval("Designation") %>' Width="32%" BackColor="Cyan"></asp:TextBox>
<asp:TextBox ID="Label6" runat="server" Text='<%#Eval("Location") %>' Width="32%" BackColor="Cyan"></asp:TextBox>
    </div>
      <asp:Repeater ID="Repeater2" runat="server" >
        <HeaderTemplate>
    <div style="background:Red">
<asp:Label ID="Label4" runat="server" Text="Employee" Width="32%"></asp:Label>
<asp:Label ID="Label5" runat="server" Text="Position" Width="32%"></asp:Label>
<asp:Label ID="Label6" runat="server" Text="City" Width="32%"></asp:Label>
    </div>
        </HeaderTemplate>

    <ItemTemplate>
    <div style="background:Yellow">
<asp:TextBox ID="TextBox4" runat="server" Text='<%#Eval("Employee") %>' Width="32%"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server" Text='<%#Eval("Position") %>' Width="32%"></asp:TextBox>
<asp:TextBox ID="TextBox6" runat="server" Text='<%#Eval("City") %>' Width="32%"></asp:TextBox>
     </div>
    </ItemTemplate>
        </asp:Repeater>

    </ItemTemplate>
    </asp:Repeater>
    </form>
</body>
</html>


Code Behind:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Mystuff.Controls
{
public partial class ReptrinsidReptr : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
dt1.Columns.Add("Name");
dt1.Columns.Add("Designation");
dt1.Columns.Add("Location");
DataRow rw1 = dt1.NewRow();
rw1["Name"] = "A";
rw1["Designation"] = ".NetDeveloper";
rw1["Location"] = "Delhi";
dt1.Rows.Add(rw1);
DataRow rw2 = dt1.NewRow();
rw2["Name"] = "B";
rw2["Designation"] = "Trainee";
rw2["Location"] = "UP";
dt1.Rows.Add(rw2);
DataRow rw3 = dt1.NewRow();
rw3["Name"] = "C";
rw3["Designation"] = "JavaDeveloper";
rw3["Location"] = "Bombay";
dt1.Rows.Add(rw3);

Repeater1.DataSource = dt1;
Repeater1.DataBind();

}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater Repeater2 = (Repeater)(e.Item.FindControl("Repeater2"));
DataTable dt2 = new DataTable();
dt2.Columns.Add("Employee");
dt2.Columns.Add("Position");
dt2.Columns.Add("City");
DataRow rw1 = dt2.NewRow();
rw1["Employee"] = "D";
rw1["Position"] = "Developer";
rw1["City"] = "USA";
dt2.Rows.Add(rw1);
DataRow rw2 = dt2.NewRow();
rw2["Employee"] = "E";
rw2["Position"] = "Trainee";
rw2["City"] = "Delhi";
dt2.Rows.Add(rw2);
DataRow rw3 = dt2.NewRow();
rw3["Employee"] = "F";
rw3["Position"] = "Sr Developer";
rw3["City"] = "Bombay";
dt2.Rows.Add(rw3);

Repeater2.DataSource = dt2;
Repeater2.DataBind();

}

}
}
}

Could anyone make me correct.if i open outer repeater inner repeater should Open.
I have done as i Mentioned above.am i wrong anywhere.
Posted
Comments
   
Here we answer specific questions which members face during programming.

We can't work on the whole source code. You need to do that yourself.
If you face any difficulty while coding, feel free to come back here and ask another question with specific issue describing the scenario.

Members will be happy to help you then.

Happy Coding. :)
Sinisa Hajnal 19-Jun-15 3:46am
   
The code looks correct at the first glance. Except you should create dt2 earlier, and just assign it to repeater2 like this: Repeater2.DataSource = dt2.copy.

As-is, you create and re-create dt2 on each Repeater1 row.

What do you get as a result? Empty repeater2?
Member 11686116 19-Jun-15 5:50am
   
no, am getting output for repeater2. I want output as if i click repeater1 then repeater2 should display.
Sinisa Hajnal 19-Jun-15 6:30am
   
Ahhhh...look at Accordion jQuery element or AjaxToolkit Accordion.
Member 11686116 19-Jun-15 6:53am
   
but i want to do it by repeater
Sinisa Hajnal 19-Jun-15 8:02am
   
Accordion simply allows you to show / hide controls you want. It doesn't affect data container. I've implemented it in one of my projects and there wasn't any problem.

Besides, you should be aware that fixating yourself on one technique limits the possibilities. If you find out there is a better/different way to do what you need, why not change?

This is not the case here, using accordion does NOT affect you, you can still use repeaters.
Member 11686116 19-Jun-15 5:52am
   
Just output looks like datasheet.i want it as repeater1 first ,once i click on repeater1 then repeater2 should display help me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900