简体   繁体   English

从儿童直放站获得父母直放站

[英]Get parent repeater from child repeater

I am writing out a list of links broken down into Year and Month like so : 我正在写一个分解为年和月的链接列表,如下所示:

<ul>
  <li>2012</li>
  <ul>
    <li>July (12)</li>
    <li>June (8)<li>
    (etc)
  </ul>
  <li>2011</li>
    <li>July (7)</li>
    (etc)
  </li>
</ul>

I am using two Repeaters, one for Years ("repYears") and a nested Repeater for Months ("repMonths"). 我正在使用两个直放站,一个用于年份(“ repYears”)和一个嵌套的直放站几个月(“ repMonths”)。 During the ItemDataBound event for repYears, I databind repMonths. 在repYears的ItemDataBound事件期间,我对repMonths进行了数据绑定。 The data comes from a Generic List of these objects: 数据来自这些对象的通用列表:

public class ArchiveItem
{
    public ArchiveItem(long Id, string Month, int Year)
    {
        PostId = Id;
        Month = Month;
        Year = Year;
    }

    public long PostId { get; set; }
    public string Month { get; set; }
    public int Year { get; set; }
}

So far I am successfully writing out the years and months. 到目前为止,我已经成功写出了年月。

What I need to do next is calculate the number of items per month (ie populate the number in the brackets from the HTML above). 接下来,我需要计算每月项目的数量(即,从上面的HTML中的括号中填充数量)。

To do this (during repMonths_ItemDataBound()) I need to know what year the repYears Repeater is currently on. 为此(在repMonths_ItemDataBound()期间),我需要知道repYears Repeater当前处于哪一年。 That will give me the current month and current year from which I can then work out the count. 这将给我当前的月份和年份,然后我便可以计算出该月份和年份。

I can access the current month easily as I am within ItemDataBound so I can use e.Item.DataItem. 我可以像访问ItemDataBound一样轻松访问当月,因此可以使用e.Item.DataItem。

So how can I access the current DataItem value from the parent Repeater? 那么如何从父Repeater访问当前的DataItem值?

I'm not sure if i get the point since you haven't shown code. 我不确定我是否明白这一点,因为您没有显示代码。

If you are in the child repeater's ItemDataBound and you need to get a reference to the parent item's DataItem , you can use the NamingContainer property: 如果您位于子转发器的ItemDataBound并且需要获取对父项的DataItem的引用,则可以使用NamingContainer属性:

in ItemDataBound of the child-repeater: 在子中继器的ItemDataBound中:

var repeater = (Repeater)sender;
var parentItem = (RepeaterItem)repeater.NamingContainer;
var parentDataItem = parentItem.DataItem;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM