Search code examples
c#asp.netrepeaterdatasourcenested-repeater

ChildRepeater getting value from ParentRepeater in CodeBehind


What i'm trying to do is this

       <asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ItemBound">
            <ItemTemplate>
               <asp:Repeater ID="Repeater_SideMenu_Guides_Medlem" runat="server">
                   <ItemTemplate>
                   </ItemTemplate>
               </asp:Repeater>
            </ItemTemplate>
        </asp:Repeater>

Codebehind

 ParentRepeater.DataSource = CraftGuides.GetAllGroups();
 ParentRepeater.DataBind();

 protected void ItemBound(object sender, RepeaterItemEventArgs args) 
    { 
        if (args.Item.ItemType == ListItemType.Item) 
        { 
            Repeater childRepeater = (Repeater)args.Item.FindControl("ChildRepeater"); 
            childRepeater.DataSource = CraftGuides.GetGuidesByGroupID( Insert ID from Parent Here ); 
            childRepeater.DataBind(); 
        } 
    }

Now, the thing is I don't know to get the ID from the parent inside the child to collect the data from the database


Solution

  • Providing that you have a Group object, you can use the following:

    var item = args.Item;
    var dataItem = item.DataItem as Group;
    

    Then you easily grab the id of the group object and pass it into your GetGuidsByGroupID().

    I like to use the as keyword since it will return null if the cast fails. Using (Group)item.DataItem would throw an exception if it failed.