Thrice Nested Repeater for Tests, Questions and Answers

Default.aspx

            <asp:Repeater runat="server" ID="rTests">
                <ItemTemplate>
                    <h3>Test: <%# DataBinder.Eval(Container.DataItem, "Name") %>, Passing Score = <%# DataBinder.Eval(Container.DataItem,"Passing_Score") %></h3>
                    <asp:Repeater ID="rQuestions" runat="server" DataSource='<%# GetChildRelation(Container.DataItem, "Test_Question")%>'>
                        <ItemTemplate>
                            <h4>Question:<%#DataBinder.Eval(Container.DataItem, "Text")%></h4>
                            <asp:Repeater ID="rAnswers" runat="server" DataSource='<%# GetChildRelation(Container.DataItem, "Question_Answer")%>'>
                                <ItemTemplate>
                                   Answer: <b><%# DataBinder.Eval(Container.DataItem, "Text") %></b> <%# DataBinder.Eval(Container.DataItem, "correct") %><br />
                                </ItemTemplate>
                            </asp:Repeater>
                        </ItemTemplate>
                    </asp:Repeater>
                </ItemTemplate>
            </asp:Repeater>

If you are having problems with button events not firing, make sure to EnableViewState.
Default.aspx.cs


    private void BindrTests()
    {
        rTests.DataSource = GetTestQuestionsAndAnswers();
        rTests.DataBind();
    }

    protected DataView GetChildRelation(object dataItem, string relation)
    {
        DataRowView drv = dataItem as DataRowView;
        if (drv != null)
            return drv.CreateChildView(relation);
        else
            return null;
    }

    public static DataSet GetTestQuestionsAndAnswers()
    {
            string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["Training"].ToString();
            string strSql = "SELECT Tests.Ident, Tests.Name, Tests.Passing_Score FROM [Training].[dbo].Tests; " +
	                        "SELECT Test_Questions.Ident, Test_Questions.Test_Ident, Test_Questions.text FROM [Training].[dbo].Test_Questions; " +
                    		"SELECT Test_Answers.Ident, Test_Answers.Question_Ident, Test_Answers.text, Test_Answers.correct FROM [Training].[dbo].Test_Answers";
            SqlConnection conn = new SqlConnection(strConn);
            SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
            da.TableMappings.Add("Tests1", "Test_Questions");
            da.TableMappings.Add("Tests2", "Test_Answers");
            DataSet dsQandA = new DataSet();
            da.Fill(dsQandA, "Tests");
            dsQandA.Relations.Add("Test_Question", dsQandA.Tables["Tests"].Columns["Ident"], dsQandA.Tables["Test_Questions"].Columns["Test_Ident"]);
            dsQandA.Relations[0].Nested = true;
            dsQandA.Relations.Add("Question_Answer", dsQandA.Tables["Test_Questions"].Columns["Ident"], dsQandA.Tables["Test_Answers"].Columns["Question_Ident"]);
            dsQandA.Relations[1].Nested = true;
            return dsQandA;
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s