gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:09:32 24 4
gpt4 key购买 nike

我有一个控件,它有一个转发器 rptReferrals,它贯穿实体对象列表 Referrals。 Referrals 对象引用了另一个名为 Answers 的表,该表是为用户提交的 Answers 列表。 rptReferrals 会将子转发器 rptQuestionnaire 绑定(bind)到我登录的人的问题列表,它未连接到它绑定(bind)到的 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 的属性。

关于如何访问我的父转发器的数据项的属性有什么想法吗?我现在会回答任何问题。

最佳答案

您可以使用 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
}
}

关于c# - 访问父转发器 DataItem 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15420819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com