Repeater in Repeater

前端 未结 7 712
离开以前
离开以前 2020-11-27 04:10

I have a repeater inside a repeater. Where the parent repeater is bound to a Datatble which has a column with a Datatable in it.

I would li

相关标签:
7条回答
  • 2020-11-27 04:49

    Here is how it's done:

    DataSource='<%# ((System.Data.DataRowView)Container.DataItem)[3] %>'
    

    So if you know the column in the parent table that holds the child table/datasource for the nested repeater you can put this directly in the aspx file.

    0 讨论(0)
  • 2020-11-27 04:50

    In the parent repeater, attach a method to the OnItemDataBound event and in the method, find the nested repeater and data bind it.

    Example (.aspx):

    <asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ItemBound">
        <ItemTemplate>
            <!-- Repeated data -->
            <asp:Repeater ID="ChildRepeater" runat="server">
                <ItemTemplate>
                    <!-- Nested repeated data -->
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </asp:Repeater>
    

    Example (.cs):

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ParentRepeater.DataSource = ...;
            ParentRepeater.DataBind();
        }
    }
    
    protected void ItemBound(object sender, RepeaterItemEventArgs args)
    {
        if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater childRepeater = (Repeater)args.Item.FindControl("ChildRepeater");
            childRepeater.DataSource = ...;
            childRepeater.DataBind();
        }
    }
    
    0 讨论(0)
  • 2020-11-27 04:53
     protected void MainRepeater_ItemDataBound(object sender, RepeaterItemEventArgs args)
        {
             if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Repeater childRepeater = (Repeater)args.Item.FindControl("ChildRepeater");
    
                    DataTable innerTable= ((DataRowView)args.Item.DataItem)["InnerTableColumnName"] as DataTable;
                    childRepeater.DataSource = tasksDetails;
                    childRepeater.DataBind();
                }
        }
    
    0 讨论(0)
  • 2020-11-27 04:55

    If I need to do that, I usually do it using the ItemDataBound event of the parent repeater to bind the child repeater. If e is your EventArgs parameter, you'll have access to the child repeater via e.Item.FindControl(), and access to the data via e.Item.DataItem.

    0 讨论(0)
  • 2020-11-27 04:55

    Repeater1 OnItemDataBound event, then FindControl Repeater2. The code-behind will not find the nested Repeater2! You have to use FindControl("Repeater2").

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.DataItem != null)
        {
            MemberView dataRow = (MemberView)e.Item.DataItem;
            var cat = MemberPresenter.getMemberID(dataRow.memID);
    
            Repeater rp2 = (Repeater)e.Item.FindControl("Repeater2");
            rp2.DataSource = cat;
            rp2.DataBind();
        }  
    }
    
    0 讨论(0)
  • 2020-11-27 04:58

    Here is an example of how to do this: Article for nested repeater control

    0 讨论(0)
提交回复
热议问题