开发者

Get Outer Repeater Item from Inner Repeater

How do you get the outer repeater item from an inner repeater. My outer repeater datasource has 3 items. My inner repeater datasource has 7 items. When I am iterating inside the inner repeater how do I get an item from the outer repeater?

My outer repeater datasource is a generic list. One of the items in this list is a day (in number format). My inner repeater is a list of int, 1 through 7 in this list. I need some logic so that when I am iteratin开发者_如何学编程g the list of days (in the inner).....if the outer has a value of 2 on the current recorded being iterated (so there is a match) I print something.

I hope that makes sense...

Thanks for any help or tips provided.


What I usually do is just catch the outer item on databound and save it to a variable on the page. Here I have two consecutive repeaters. I save the year from the first repeater and then can reference it when I'm binding the second one.

    protected void repAnnualReport_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        CurrentYear = int.Parse(((Literal)e.Item.FindControl("litLicenseYear")).Text);

        Repeater repLicenseLengths = (Repeater)e.Item.FindControl("repLicenseLengths");
        repLicenseLengths.DataSource = GetLicenseLengths(CurrentYear);
        repLicenseLengths.DataBind();
    }

    protected void repLicenseLengths_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        CurrentLength = int.Parse(((Literal)e.Item.FindControl("litLicenseLength")).Text) * 365;

        Repeater repMonthlyReport = (Repeater)e.Item.FindControl("repMonthlyReport");
        repMonthlyReport.DataSource = new object[12];
        repMonthlyReport.DataBind();
    }

If what your binding to the first repeater is an class list or queryable, you can access the individual item like this in the first line.

SaveCurrentItem = (CurrentItemClass)e.Item.DataItem;


If you know the hierarchy of .net controls from your inner repeater to the outer repeater, you can use the NamingContainer property to find your way up.

    protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            Repeater parentRepeater;

            // e.Item: the item/header/whatever template that kicked off this event
            // e.Item.NamingContainer: the owner of the item template (the innner repeater)
            // e.Item.NamingContainer.NamingContainer: the outer item template
            // e.Item.NamingContainer.NamingContainer.NamingContainer: the outer Repeater

            parentRepeater = (Repeater)e.Item.NamingContainer.NamingContainer.NamingContainer;

        }
    }

Otherwise if you're unsure of the structure or don't want the fixed reference like this, you can loop your way up through the NamingContainer until you hit something of type Repeater for the second time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜