Search code examples
c#asp.net.netgridviewrepeater

Insert table in a single cell inside repeater


I am trying to build a table structure using asp.net Repeater like this below:

        column 1      |      Column 2

Row1      cell1               cell2
---------------------------------------
       TABLE 1                 TABLE 2
    ----------------------------------
        col1|Col2|Col3_     same  column and rows are here as well   
Row2    row1____|____|____  
        row2___ |____|_____  
        row3____|____|_____

But I got stuck in adding Table 1 and Table 2 for the Row 2. I am not sure how to add the table in a single cell inside the Repeater and the data needs to binding from the DataTable.

And below is my code for Repeater:

<asp:Repeater ID="Repeaterp" runat="server">
    <HeaderTemplate>
        <table>
            <tr><th>usedcount</th><th>notUsedCount</th></tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><asp:TextBox runat="server" ID="txtAvai" Text='<%#Eval("Count") %>' ReadOnly="true"></asp:TextBox></td>
            <td><asp:TextBox runat="server" ID="txtConv" Text='' ReadOnly="true"></asp:TextBox></td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater

Could any one please suggest any idea on this one that would be very grateful to me?


Solution

  • You can nest different asp.net Data Representation controls (e.g. asp:Repeater, asp:DataList, asp:GridView or asp:Table etc.) inside a Repeater control. I have added a quick example for making a nested structure with multiple Repeater controls:

    .Aspx Code:

    <asp:Repeater ID="RepeaterTable" OnItemDataBound="RepeaterTable_ItemDataBound" runat="server">
        <HeaderTemplate>
            <table>
                <tr>
                    <th>Column 1</th>
                    <th>Column 2</th>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <asp:Panel ID="PanelTextBoxes" runat="server">
                <tr>
                    <td>
                        <asp:TextBox ID="txtAvai" Text='<%# Eval("Count") %>' runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="txtConv" Text='' runat="server"></asp:TextBox>
                    </td>
                </tr>
            </asp:Panel>
            <asp:Panel ID="PanelTables" runat="server">
                <tr>
                    <td>
                        <asp:Repeater ID="RepeaterTable1" OnItemDataBound="RepeaterTable1_ItemDataBound" runat="server">
                            <HeaderTemplate>
                                <table>
                                    <tr>
                                        <th>T1 Col 1</th>
                                        <th>T1 Col 2</th>
                                    </tr>
                            </HeaderTemplate>
                            <ItemTemplate>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblCol1" runat="server" Text='<%# Eval("Col1") %>'></asp:Label>
                                        </td>
                                        <td>
                                            <asp:Label ID="lblCol2" runat="server" Text='<%# Eval("Col2") %>'></asp:Label>
                                        </td>
                                    </tr>
                            </ItemTemplate>
                            <FooterTemplate>
                                </table>
                            </FooterTemplate>
                        </asp:Repeater>
                    </td>
                    <td>
                        <asp:Repeater ID="RepeaterTable2" OnItemDataBound="RepeaterTable2_ItemDataBound" runat="server">
                            <HeaderTemplate>
                                <table>
                                    <tr>
                                        <th>T2 Col 1</th>
                                        <th>T2 Col 2</th>
                                    </tr>
                            </HeaderTemplate>
                            <ItemTemplate>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblCol1" runat="server" Text='<%# Eval("Col1") %>'></asp:Label>
                                        </td>
                                        <td>
                                            <asp:Label ID="lblCol2" runat="server" Text='<%# Eval("Col2") %>'></asp:Label>
                                        </td>
                                    </tr>
                            </ItemTemplate>
                            <FooterTemplate>
                                </table>
                            </FooterTemplate>
                        </asp:Repeater>
                    </td>
                </tr>
            </asp:Panel>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    

    .Aspx.cs Code:

    DataTable TempDT = new DataTable();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getData();
        }
    }
    
    // create DataTable 3 x 2
    public void getData()
    {
        TempDT = new DataTable();
        TempDT.Columns.Add("Col1");
        TempDT.Columns.Add("Col2");
        TempDT.Columns.Add("Count");
        TempDT.Rows.Add("Temp", "Temp", 100);
        TempDT.Rows.Add("Temp", "Temp", 100);
        TempDT.Rows.Add("Temp", "Temp", 100);
    
        // store DataTable into ViewState from lost on PostBack
        ViewState["DT"] = TempDT;
    
        RepeaterTable.DataSource = TempDT;
        RepeaterTable.DataBind();
    }
    
    // Calls parent Repeater on Binding Data
    protected void RepeaterTable_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        // check Repeater item type is not in edit mode
        if (e.Item.ItemType == ListItemType.Item || 
            e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataTable dt = new DataTable();
    
            // get and set DataTable from ViewState
            dt = ViewState["DT"] as DataTable;
    
            Repeater RepeaterTable1 = e.Item.FindControl("RepeaterTable1") as Repeater;
            Repeater RepeaterTable2 = e.Item.FindControl("RepeaterTable2") as Repeater;
    
            RepeaterTable1.DataSource = dt;
            RepeaterTable1.DataBind(); // calls RepeaterTable1_ItemDataBound event
    
            RepeaterTable2.DataSource = dt;
            RepeaterTable2.DataBind(); // // calls RepeaterTable2_ItemDataBound event
    
            Panel PanelTextBoxes = e.Item.FindControl("PanelTextBoxes") as Panel;
            Panel PanelTables = e.Item.FindControl("PanelTables") as Panel;
    
            // show only first structure
            if (e.Item.ItemIndex != 0)
            {
                PanelTextBoxes.Visible = false;
                PanelTables.Visible = false;
            }        
        }
    }
    
    // Calls child Repeater on Binding Data
    protected void RepeaterTable1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        // check Repeater item type is not in edit mode
        if (e.Item.ItemType == ListItemType.Item || 
            e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //.. here is code when child repeater is binding
        }
    }
    
    // Calls child Repeater on Binding Data
    protected void RepeaterTable2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        // check Repeater item type is not in edit mode
        if (e.Item.ItemType == ListItemType.Item || 
            e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //.. here is code when child repeater is binding
        }
    }
    

    A Demo Image is:

    enter image description here

    Update:

    If you don't want to repeat the whole structure then just add below code in RepeaterTable_ItemDataBound event:

    Panel PanelTextBoxes = e.Item.FindControl("PanelTextBoxes") as Panel;
    Panel PanelTables = e.Item.FindControl("PanelTables") as Panel;
    
    if (e.Item.ItemIndex != 0)
    {
        PanelTextBoxes.Visible = false;
        PanelTables.Visible = false;
    }
    

    Not repeating the whole structure image demo:

    enter image description here