2

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

Michael Tot Korsgaard
  • 3,582
  • 9
  • 47
  • 84
  • When you say "I don't know [how] to get the ID from the parent inside the child", are you talking about the `ParentRepeater`? If so, how many repeaters do you have on your page? Normally, I would just reference `ParentRepeater` directly. – jwheron Nov 01 '11 at 20:51
  • I'm not sure what you mean. What I need from the ParentRepeater is the id of the object from CraftGuides.GetAllGroups(). If that is the same as what you are talking about, can you then make an example – Michael Tot Korsgaard Nov 01 '11 at 23:02

1 Answers1

3

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.

Digbyswift
  • 9,945
  • 2
  • 36
  • 65
  • I'm not quite following ya, can you make an example because i'm new to c# ^^ – Michael Tot Korsgaard Oct 31 '11 at 16:40
  • If I understand your clarification in the comments above, then Digbyswift's example is correct, and that C# code pasted above is what you need. – jwheron Nov 02 '11 at 00:36
  • 1
    I will add, though, that I do not like `as` for the same reason that Digbyswift likes it. I'd rather get the `NullReferenceException` than mysteriously see an empty `Repeater` or get another exception farther away from the source. – jwheron Nov 02 '11 at 14:19