Access Parent Repeaters DataItem Property

2019-08-17 14:52发布

我有一个有中继器,rptReferrals,通过实体对象的列表,转诊运行的控制。 请示对象必须名为回答另一个表,这是得到了提交给用户答案的​​列表的引用。 rptReferrals将孩子中继器,rptQuestionnaire绑定到我登录时的人,这是不连接到介绍人问题的列表势必反对。

下面是ASPX代码:

<asp:Repeater runat="server" ID="rptReferrals" OnItemDataBound="rptReferrals_OnItemDataBound">
    <ItemTemplate>
        //some HTML for the referral object
        <asp:Repeater runat="server" ID="rptQuestionnaire" OnItemDataBound="rptQuestionnaire_OnItemDataBound">
              //some HTML for displaying questions and answers
        </asp:Repeater>
    </ItemTemplate>
 </asp:Repeater>

后端代码:

protected void rptReferrals_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
         //THIS IS THE ITEM THAT HAS THE LIST OF ANSWERS I NEED
         var data = e.Item.DataItem as PatientReferral;
         var rptQuestionnaire = e.Item.FindControl("rptQuestionnaire") as Repeater;
         rptQuestionnaire.DataSource = QuestionList;   
         rptQuestionnaire.DataBind();

         //QuestionList is a list of questions populated on page load.
         // I can't bind to the property of data.Answers because not
         //all questions are answered. data.Answers is only a list of 
         //the questions answered
    }
}

protected void rptQuestionnaire_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
       Need to access the data.Answers object from above. HOW????
    }
}

我遇到的问题是不需要的问题清单,我需要,无论用户是否回答,或不显示所有的问题。 但是,如果他们没有回答这个问题,我需要显示的答案,这是连接到rptReferrals财产。

关于如何访问我的父母中继的DataItem的的性能的任何想法? 我会采取任何答案在这一点上。

Answer 1:

您可以使用.Parent的属性RepeaterItem控制,以您的方式工作到外RepeaterItem (因此,它的DataItem )。

看起来这会工作:

protected void rptQuestionnaire_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
       Repeater currentRepeater = (Repeater)sender;
       // Note that you might only need one ".Parent" here.  Or you might need
       // more, depends on your actual markup.
       var data = ((RepeaterItem)e.Item.Parent.Parent).DataItem as PatientReferral;
       // Now you have access to data.Answers from the parent Repeater
    }
}


文章来源: Access Parent Repeaters DataItem Property