c# – 访问父转发器DataItem属性

前端之家收集整理的这篇文章主要介绍了c# – 访问父转发器DataItem属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个控件,它有一个Repeater,rptReferrals,它通过一个Entity对象列表,引用. Referrals对象引用了另一个名为Answers的表,它是为用户提交的Answers列表. rptReferrals会将子转发器rptQuestionnaire绑定到我登录的人的问题列表,该列表未连接到它所绑定的Referrals对象.

这是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的属性的任何想法?此时我会回答任何问题.

解决方法

您可以使用RepeaterItem控件的.Parent属性直到外部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
    }
}
原文链接:https://www.f2er.com/csharp/92322.html

猜你在找的C#相关文章