7

我有一个控件,它有一个Repeater,rptReferrals,它贯穿一个实体对象列表,Referrals。Referrals 对象具有对另一个名为 Answers 的表的引用,该表是为用户提交的 Answers 列表。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 的属性。

关于如何访问我的父中继器数据项的属性的任何想法?在这一点上,我会接受任何答案。

4

1 回答 1

12

您可以使用控件的.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
    }
}
于 2013-03-15T06:20:38.920 回答