C#中的嵌套中继器

时间:2013-07-01 06:07:15

标签: asp.net c#-4.0 repeater

您好我必须在转发器中显示分层信息(有四个级别)。为此,我决定使用嵌套的转发器控件。 我在MSDN上发现了这篇文章http://support.microsoft.com/kb/306154,它展示了如何将嵌套转发器用于两个级别的信息。 有人可以帮我把它扩展到四个级别吗? 一个示例代码将非常适合。谢谢。

4 个答案:

答案 0 :(得分:15)

HTML CODE:

<asp:Repeater ID="Repeater1" runat="server" 
        onitemdatabound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <h1>
                Repeater 1</h1>
            <asp:Repeater ID="Repeater2" runat="server" onitemdatabound="Repeater2_ItemDataBound">
                <ItemTemplate>
                    <h1>
                        Repeater 2
                    </h1>
                    <asp:Repeater ID="Repeater3" runat="server" onitemdatabound="Repeater3_ItemDataBound">
                        <ItemTemplate>
                            <h1>
                                Repeater 3
                            </h1>
                            <asp:Repeater ID="Repeater4" runat="server" onitemdatabound="Repeater4_ItemDataBound">
                                <ItemTemplate>
                                    <h1>
                                        Repeater 4
                                    </h1>
                                </ItemTemplate>
                            </asp:Repeater>
                        </ItemTemplate>
                    </asp:Repeater>
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </asp:Repeater>

C#代码:

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt1 = new DataTable();
        //Need to assign the Data in datatable
        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();
            //Need to assign the Data in datatable
            Repeater2.DataSource = dt2;
            Repeater2.DataBind();
        }

    }
    protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater Repeater3 = (Repeater)(e.Item.FindControl("Repeater3"));

            DataTable dt3 = new DataTable();
            //Need to assign the Data in datatable
            Repeater3.DataSource = dt3;
            Repeater3.DataBind();
        }

    }

    protected void Repeater3_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater Repeater4 = (Repeater)(e.Item.FindControl("Repeater4"));

            DataTable dt4 = new DataTable();
            //Need to assign the Data in datatable
            Repeater4.DataSource = dt4;
            Repeater4.DataBind();
        }

    }
}

答案 1 :(得分:0)

答案 2 :(得分:0)

在第一个答案的基础上,您可以在Page_Load上传递表数据,将其设置为ViewState变量,然后在绑定时检索它,而不是在ItemDataBound函数中构建表:

private DataTable GetCachedDataTable(string strTable)
{
    DataTable dtableCached = (DataTable)this.ViewState[strTableCache];
    return dtableCached;
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.ViewState["TblTwo_Cache"] = null;
        DataTable tblOne = new DataTable();
        DataTable tblTwo = new DataTable();
        myFunctionReturningTwoTables(ref tblOne, ref tblTwo);

        // Bind the first one
        if (tblOne != null)
        {
            // This first line assumes an <asp:Panel ID=pnlMain runat=server> 
            // tag is added in front of the Repeater1 tag in the ASPX markup, above,
            // and an </asp:Panel> tag is after the last </asp:Repeater> tag
            Repeater rptr = pnlMain.FindControl("Repeater1") as Repeater;
            rptr.ItemDataBound += new RepeaterItemEventHandler(rptrItemDataBound);
            rptr.DataSource = tblOne;
            rptr.DataBind();
        }
        // Cache the 2nd (and others...) like this
        if (tblTwo != null)
        {
            this.ViewState["TblTwo_Cache"] = tblTwo;
        }
    }
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater rptr2 = e.Item.FindControl("Repeater2") as Repeater;
        if (rptr2 != null)
        {
            DataTable dt = new DataTable();
            // Now, pull it out of cache
            dt = GetCachedDataTable("TblTwo_Cache");
            if (dt != null)
            {
                rptr2.DataSource = dt;
                rptr2.DataBind();
            }
        }
    }
}

答案 3 :(得分:0)

如果您有强类型数据类型,最好使用ItemType控件中提供的<asp:Repeater>属性,以便您可以轻松设置嵌套DataSource的{​​{1}}属性按照每个嵌套转发器的相同步骤控制<asp:Repeater>

代码示例:

&#13;
&#13;
<%#Container.DataItem %>
&#13;
&#13;
&#13;