Access Parent Repeaters DataItem Property

流过昼夜 提交于 2019-12-04 20:51:34

问题


I have a control that has a Repeater, rptReferrals, that runs through a list of Entity objects, Referrals. The Referrals object has a reference to another table called Answers, which is a list of Answers that got submitted for the user. rptReferrals will bind a child repeater, rptQuestionnaire to a List of Questions for the person I am logged in with, which is not connected to the Referrals object it is bound to.

Here is the aspx code:

<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>

The backend code:

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????
    }
}

The problem I am having is that the list of questions are not required and I need to display all the questions regardless of whether the user answered it or not. But if they did answer it, I need to display the answer, which is the property attached to rptReferrals.

Any ideas on how to access a property of the dataitem of my parent repeater? I'll take any answer at this point.


回答1:


You can use the .Parent attribute of the RepeaterItem control to work your way up to the outer RepeaterItem (and, thus, its DataItem).

Seems like this would work:

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
    }
}


来源:https://stackoverflow.com/questions/15420819/access-parent-repeaters-dataitem-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!