Click here to Skip to main content
14,265,888 members
Rate this:
Please Sign up or sign in to vote.
See more:
The web application that I am developing right now has something called quiz engine which provides users with short quizzes which consist of one question or more. Now, I have a problem with the QUIZ page. The quiz may consist of one question or more. The main problem that I have is following: If the quiz consists of one question, when the user comes to this quiz, he will see the NEXT button instead of seeing the FINISHED button. However, if the quiz consists of more than one question, he will see the FINISHED button in the page with the last question. I don't know why this is happening with me. Any help please?


For creating the Quiz engine, I used the Toturial in the ASP.NET website for creating it.

My ASP.NET code:
<asp:DetailsView ID="questionDetails" runat="server" Height="50px" Width="550px" AutoGenerateRows="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" />
                    <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" CssClass="boldtext" Width="80px" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <Fields>
                        <asp:BoundField DataField="Question" HeaderText="Question:" SortExpression="Question" />
                        <asp:BoundField DataField="Answer1" HeaderText="A:" SortExpression="Answer1" />
                        <asp:BoundField DataField="Answer2" HeaderText="B:" SortExpression="Answer2" />
                        <asp:BoundField DataField="Answer3" HeaderText="C:" SortExpression="Answer3" />
                        <asp:BoundField DataField="Answer4" HeaderText="D:" SortExpression="Answer4" />
                    </Fields>
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#999999" />
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="generaltext" />
                </asp:DetailsView>
                 
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT [QuestionID], [Question], [Answer1], [Answer2], [Answer3], [Answer4], [CorrectAnswer], [QuestionOrder] FROM [Question] WHERE ([QuizID] = @QuizID) ORDER BY [QuestionOrder]">
                    <SelectParameters>
                        <asp:SessionParameter SessionField="QuizID" Type="Int32" Name="QuizID" DefaultValue="0" />
                    </SelectParameters>
                </asp:SqlDataSource>






My code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    questionDetails.DataBind();
}

protected void nextButton_Click(object sender, EventArgs e)
{
    // Save off previous answers
    System.Data.DataRowView dr = (System.Data.DataRowView)questionDetails.DataItem;

    // Create Answer object to save values
    Answer a = new Answer();
    a.QuestionID = dr["QuestionOrder"].ToString();
    a.CorrectAnswer = dr["CorrectAnswer"].ToString();
    a.UserAnswer = answerDropDownList.SelectedValue.ToString();

    ArrayList al = (ArrayList)Session["AnswerList"];
    al.Add(a);

    Session.Add("AnswerList", al);

    if (questionDetails.PageIndex == questionDetails.PageCount - 1)
    {
        // Go to evaluate answers
        Response.Redirect("Results.aspx");
    }
    else
    {
        questionDetails.PageIndex++;
    }

    if (questionDetails.PageIndex == questionDetails.PageCount - 1)
    {
        nextButton.Text = "Finished";
    }

}



**SO HOW TO FIX THIS PROBLEM?**
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Use single button for showing Next and finished and check on PageLoad whether there are more than one questions or only one.If there is only one question change the button text to Finished otherwise change text to Next.
Similarly check on Next button click event whether there are more questions or not,if not then change it to Finished.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100