在中继器内的单个单元格中插入 table

Insert table in a single cell inside repeater

我正在尝试使用 asp.net 转发器构建一个 table 结构,如下所示:

        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____|____|_____

但是我在为 [=21= 添加 Table 1Table 2 时遇到了困难]第 2 行。我不确定如何在 Repeater 内的单个单元格中添加 table 并且数据需要从 DataTable.

绑定

下面是我的 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

任何人都可以就此提出任何想法吗?

如果你真的想在中继器的第二行有一个table,你可以这样做。

将两个PlaceHolder添加到ItemTemplate。一个用于带有 table 的第二行,一个用于其他行。根据 ItemIndex 设置他们的可见性基础。请注意,使用 GridView 是因为它们成为 HTML.

中的 table 元素
<ItemTemplate>

    <asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# Container.ItemIndex != 1 %>'>
        <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>
    </asp:PlaceHolder>

    <asp:PlaceHolder ID="PlaceHolder2" runat="server" Visible='<%# Container.ItemIndex == 1 %>'>
        <tr>
            <td>
                <asp:GridView ID="GridView1" runat="server"></asp:GridView>
            </td>
            <td>
                <asp:GridView ID="GridView2" runat="server"></asp:GridView>
            </td>
        </tr>
    </asp:PlaceHolder>

</ItemTemplate>

如果您希望第 3 行再次成为这 2 个文本框,然后第 4 行成为 tables 等,请在可见的 属性 中使用 Container.ItemIndex % 2 == 0Container.ItemIndex % 2 == 1 PlaceHolder,因为上面的演示假设 Repeater 中只有 2 行。

接下来,将 OnItemDataBound 事件添加到 Repeater。

<asp:Repeater ID="Repeaterp" runat="server" OnItemDataBound="Repeaterp_ItemDataBound">

然后在后面的代码中查看被绑定的项目是否为第二行,找到GridViews 并将数据绑定到它们。我为此演示创建了一个虚拟 DataTable,但您可以在 Repeaterp_ItemDataBound 方法

中将任何源绑定到它们
protected void Repeaterp_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    //check if it is the second row
    if (e.Item.ItemIndex == 1)
    {
        //find the gridviews in the repeater item using findcontrol
        GridView gv1 = e.Item.FindControl("GridView1") as GridView;
        GridView gv2 = e.Item.FindControl("GridView2") as GridView;

        //create a dummy datatable for this demo
        DataTable table = new DataTable();
        table.Columns.Add("Col1", typeof(int));
        table.Columns.Add("Col2", typeof(string));
        table.Columns.Add("Col3", typeof(string));

        //add some rows to the table
        table.Rows.Add(0, "Row 1", "AAA");
        table.Rows.Add(1, "Row 2", "BBB");
        table.Rows.Add(2, "Row 3", "CCC");

        //bind the data to the gridviews in the second row
        gv1.DataSource = table;
        gv2.DataSource = table;

        gv1.DataBind();
        gv2.DataBind();
    }
}

您可以在 Repeater 控件中嵌套不同的 asp.net 数据表示控件(例如 asp:Repeaterasp:DataListasp:GridViewasp:Table 等)。我添加了一个快速示例,用于制作具有多个 Repeater 控件的嵌套结构:

.Aspx代码:

<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 代码:

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
    }
}

演示图像是:

更新:

如果您不想重复整个结构,则只需在 RepeaterTable_ItemDataBound 事件中添加以下代码:

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;
}

不重复整个结构图像演示:

为什么要在转发器的第二项中添加两个 table? 不需要 Repeater==Table

相反,在中继器的 <headertemplate> 中放置主 table 的第一行,第二行包含您想要的所有 2 table。然后将中继器的 <ItemTemplate> 保留为其余行(从第 3 行向下)

您可以通过后面的代码访问这两个 table 或使用属性或 Eval 设置值

您的 .aspx 可能如下所示: (我为中继器添加了 XmlDataSource1 只是为了使绑定有效,我还使用了 属性 <%= this.ContentString %> ,我将在后面的代码中声明和设置它)

   <asp:Repeater ID="Repeaterp" runat="server" DataSourceID="XmlDataSource1">
        <HeaderTemplate>
            <table>
                <%--------Your Master Table--------%>
                <tr>
                    <th>usedcount
                    </th>
                    <th>notUsedCount
                    </th>
                </tr>
                <tr>
                    <td>Row1 Cell1</td>
                    <td>Row1 Cell2</td>
                </tr>
                <tr>
                    <td>
                        <%----------------First Inner Table------------------%>
                        <asp:Table ID="Table1" runat="server">
                            <asp:TableHeaderRow>
                                <asp:TableHeaderCell>
                        Header
                                </asp:TableHeaderCell>
                                <asp:TableHeaderCell>
                        Header
                                </asp:TableHeaderCell>
                            </asp:TableHeaderRow>
                            <asp:TableRow>
                                <asp:TableCell>
                        <%---Add your conents as properties----%>
                                 <%= this.ContentString %>

                                </asp:TableCell>
                                <asp:TableCell>

                                </asp:TableCell>
                            </asp:TableRow>

                            <asp:TableRow>
                                <asp:TableCell>                         
                                    content
                                </asp:TableCell>
                                <asp:TableCell>
                                    content
                                </asp:TableCell>
                            </asp:TableRow>
                            <asp:TableRow>
                                <asp:TableCell>                         
                                    content
                                </asp:TableCell>
                                <asp:TableCell>
                                    content
                                </asp:TableCell>
                            </asp:TableRow>
                        </asp:Table>

                    </td>
                    <td>

                        <%----------------Second Inner Table------------------%>
                        <asp:Table ID="Table2" runat="server">
                            <asp:TableHeaderRow>
                                <asp:TableHeaderCell>
                        Header
                                </asp:TableHeaderCell>
                                <asp:TableHeaderCell>
                        Header
                                </asp:TableHeaderCell>
                            </asp:TableHeaderRow>
                            <asp:TableRow>
                                <asp:TableCell>
                        <%---Add your conents as properties----%>
                                 <%= this.ContentString %>

                                </asp:TableCell>
                                <asp:TableCell>

                                </asp:TableCell>
                            </asp:TableRow>

                            <asp:TableRow>
                                <asp:TableCell>                         
                                    content
                                </asp:TableCell>
                                <asp:TableCell>
                                    content
                                </asp:TableCell>
                            </asp:TableRow>
                            <asp:TableRow>
                                <asp:TableCell>                         
                                    content
                                </asp:TableCell>
                                <asp:TableCell>
                                    content
                                </asp:TableCell>
                            </asp:TableRow>
                        </asp:Table>
                    </td>
                </tr>
                <%-- Closing the second row of master table --%>
                <%-- Everything is completed in the repeater's header! --%>
        </HeaderTemplate>

        <ItemTemplate>
            <tr>
                <td><%--continue master table as usual--%> </td>
                <td></td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </Table>
        </FooterTemplate>
    </asp:Repeater>

这是后面的代码,请注意 属性 ContentString。以及如何在绑定中继器后访问第 2 行的 table:

                public partial class _Default : Page
                {
                    private string strContent;

                    // notice the property that the tables can read as in the aspx code above
                    public String ContentString
                    {
                        get { return strContent; }
                    }

                    protected void Page_Load(object sender, EventArgs e)
                    {
                        strContent = "Your Content";

                        Repeaterp.DataBind();
                        // here's how to access the two tables
                        Table Table1 = (Table)Repeaterp.Controls[0].FindControl("Table1");
                        Table Table2 = (Table)Repeaterp.Controls[0].FindControl("Table2");
                    }
                }

我会告诉你怎么做的逻辑,而不是因为我最近很忙而写代码。

1) 将 ItemDataBound 事件添加到您的父转发器(假设 id="parentrepeater".

2) 在 aspx 文件中的转发器项目模板中添加子转发器(假设 id="childrepeater".

3) 在父转发器ItemDataBound中,找到你的子转发器并绑定数据源。

protected void parent_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)
    {
        Repeater childRepeater = e.Item.FindControl("childrepeater") as Repeater;
        childRepeater.DataSource = "Get Your Datasource here";
        childRepeater.DataBind();  
    }
}

使用此方法可以实现无限多级嵌套转发器