我有一个控件,它有一个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属性的任何想法?我现在会接受任何答复。
答案 0 :(得分:11)
您可以使用.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
}
}