这是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 }}总结
以上是内存溢出为你收集整理的c# – 访问父转发器DataItem属性全部内容,希望文章能够帮你解决c# – 访问父转发器DataItem属性所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)