使用中继器列出调查的子标题
本文关键字:标题 调查 中继器 | 更新日期: 2023-09-27 18:25:16
数据库表和样本数据:
ProductTable:
Id - Name - Description - Price
1, COmpact disc, Empty Cds, 10.00
SurveySubTitleTable:
ID - ProductID - Text
1, 1, Personal Information
2, 1, Favorite Products
3, 1, Feedback
Survey
ID - ProductID - SurveySubTitleID - SurveyQuestion
1, 1, 1, What is your name
2, 1, 1, What is your surname
3, 1, 2, Name your favorite products
4, 1, 3, Feedback on this product
5, 1, 3, How was our service
需要的结果
Personal Information
What is your name
What is your surname
Favorite Products
Name your favorite products
Feedback
Feedback on this product
How was our service
当用户到达该页面时,我会通过Survery和产品ID获取数据。
我试过使用两个中继器嵌套
<asp:Repeater ID="repeater1" runat="server" OnItemDataBound="repeater1_ItemDataBound">
<ItemTemplate>
<asp:Label ID="SubTitleLabel" runat="server" Text=""></asp:Label>
<asp:Repeater ID="SurveyQuestions" runat="server" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
然而,我使用的方法(摘自http://www.codeproject.com/Articles/6140/A-quick-guide-to-using-nested-repeaters-in-ASP-NET)如果子标题有多个问题,则似乎重复该子标题。
因此,由于我通过产品id获得了调查问题,我不确定这是否是错误的方法,或者是否有更好的方法来实现这一点?感谢在这方面的任何帮助。
您可以通过子句将linq与group一起使用,即
repeater1.DataSource = YourData.GroupBy( e=> e.SurveySubTitleText )
repeater1.DataBind()
<asp:Repeater ID="repeater1" runat="server" OnItemDataBound="repeater1_ItemDataBound">
<ItemTemplate>
<asp:Label ID="SubTitleLabel" runat="server" Text='<%# Eval("Key") %>'></asp:Label>
<asp:Repeater ID="SurveyQuestions" runat="server" datasource="<%# Container.DataItem %>" >
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
其中SurveySubTitleText是副标题的文本。