Search code examples
c#asp.netlinq-to-sqlrepeater

Using repeater to list sub title with survey


Database tables and sample data:

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

Outcome required

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

When a user arrives to this page i get data by Survery and product ID.

I have tried using two repeaters nested

<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>

However the method im using (picked up from http://www.codeproject.com/Articles/6140/A-quick-guide-to-using-nested-repeaters-in-ASP-NET) seems to repeat the sub title if that sub title has more than one question.

So since im getting the survery questions by product id - im not sure if this is the wrong approach or whether theres a better way to achieve this? Appreciate any help on this.


Solution

  • You can use linq with group by clausole i.e

    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>

    Where SurveySubTitleText is the text of the subtitle.