使用转发器列出带有调查的子标题

时间:2014-10-01 11:28:47

标签: c# asp.net linq-to-sql repeater

数据库表和示例数据:

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获得了问题 - 我不确定这是否是错误的方法,或者是否有更好的方法来实现这一目标?感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

你可以使用linq和clausole分组,即

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是副标题的文本。