I have a case of nested repeaters where a child repeater is nested in the ItemTemplate of a parent repeater. The DataSource of the parent is a Dictionary<String, List<XYZ>>.
In the ItemDataBound of the parent Repeater I am using the full code:
protected void rptParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.DataItem is KeyValuePair<String, List<Object>>)
{
pair = (KeyValuePair<String, List<XYZ>>)e.Item.DataItem;
}
Repeater childRepeater = e.Item.FindControl("rptChild") as Repeater;
//bind the child repeater.
childRepeater.ItemDataBound += new RepeaterItemEventHandler(childRepeater_ItemDataBound);
childRepeater.DataSource = pair.Value;
childRepeater.DataBind();
}
}
protected void childRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
//Access the Parent row's Key value
}
}
There are 2 questions:
-
Can I use a hidden field while binding the parent and set it to the Key value and then retrieve the hidden field value in the child?
-
Will the order of the events firing be as follows:
a. Parent_ItemDataBound for Row 1 of Dictionary<Key, List<XYZ>>
i. Child_ItemDataBound for each child row of Row 1 of parent repeater
ii. Child_ItemDataBound for Footer of child repeater
b. Parent_ItemDataBound for Row 2 of Dictionary<Key, List<XYZ>>
i. Child_ItemDataBound for each child row of Row 2 of parent repeater
ii. Child_ItemDataBound for Footer of child repeater
and so on. In other words, will the Parent_ItemDataBound be followed by Child_ItemDataBound events for each child row – with the cycle repeating?
Here are the answers to your question:
Yes, you can access parent repeater item control
var hfID = e.Item.NamingContainer.NamingContainer.FindControl(“hfID”) as HiddenField;
the order is correct.
Hope that helps