-2

I have created a multiple choice quiz in asp but the code i have is very long, do you know any way of making it shorter. Also i have validators for each of the inputs but im not sure how i can stop the submit button from being clickable unless all of the answers are input.

my test code:

<%@ Page Title="" Language="C#" MasterPageFile="~/SyntherMaster.Master" AutoEventWireup="true" CodeBehind="Moments and Energy Test.aspx.cs" Inherits="ComputingProject.RevisionPages._4._Nuclear_and_Particle_Physics.Nuclear_and_Particle_Physics_Topic_List" %>



<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <asp:Label ID="QLabel1" runat="server" Text="Question 1"></asp:Label>
    <asp:RadioButtonList ID="RadioButtonList1" runat="server"
        RepeatDirection="Horizontal" RepeatLayout="Table">
        <asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
        <asp:ListItem Text="*Answer 2*" Value="Correct" />
        <asp:ListItem Text="*Answer 3*" Value="I" />
        <asp:ListItem Text="*Answer 4*" Value="4" />
    </asp:RadioButtonList>
    <div>
        <asp:RequiredFieldValidator ID="Q1requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList1" ErrorMessage="Ensure question 1 is completed"></asp:RequiredFieldValidator>
    </div>

    <asp:Label ID="QLabel2" runat="server" Text="Question 2"></asp:Label>
    <asp:RadioButtonList ID="RadioButtonList2" runat="server"
        RepeatDirection="Horizontal" RepeatLayout="Table">
        <asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
        <asp:ListItem Text="*Answer 2*" Value="Correct" />
        <asp:ListItem Text="*Answer 3*" Value="I" />
        <asp:ListItem Text="*Answer 4*" Value="4" />
    </asp:RadioButtonList>
    <div>
        <asp:RequiredFieldValidator ID="Q2requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList2" ErrorMessage="Ensure question 2 is completed"></asp:RequiredFieldValidator>
    </div>

    <asp:Label ID="QLabel3" runat="server" Text="Question 3"></asp:Label>
    <asp:RadioButtonList ID="RadioButtonList3" runat="server"
        RepeatDirection="Horizontal" RepeatLayout="Table">
        <asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
        <asp:ListItem Text="*Answer 2*" Value="Correct" />
        <asp:ListItem Text="*Answer 3*" Value="I" />
        <asp:ListItem Text="*Answer 4*" Value="4" />
    </asp:RadioButtonList>
    <div>
        <asp:RequiredFieldValidator ID="Q3requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList3" ErrorMessage="Ensure question 3 is completed"></asp:RequiredFieldValidator>
    </div>
    <asp:Label ID="QLabel4" runat="server" Text="Question 4"></asp:Label>
    <asp:RadioButtonList ID="RadioButtonList4" runat="server"
        RepeatDirection="Horizontal" RepeatLayout="Table">
        <asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
        <asp:ListItem Text="*Answer 2*" Value="Correct" />
        <asp:ListItem Text="*Answer 3*" Value="I" />
        <asp:ListItem Text="*Answer 4*" Value="4" />
    </asp:RadioButtonList>
    <div>
        <asp:RequiredFieldValidator ID="Q4requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList4" ErrorMessage="Ensure question 4 is completed"></asp:RequiredFieldValidator>
    <asp:Label ID="QLabel5" runat="server" Text="Question 5"></asp:Label>
    <asp:RadioButtonList ID="RadioButtonList5" runat="server"
        RepeatDirection="Horizontal" RepeatLayout="Table">
        <asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
        <asp:ListItem Text="*Answer 2*" Value="Correct" />
        <asp:ListItem Text="*Answer 3*" Value="I" />
        <asp:ListItem Text="*Answer 4*" Value="4" />
    </asp:RadioButtonList>
        <asp:RequiredFieldValidator ID="Q5requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList5" ErrorMessage="Ensure question 5 is completed"></asp:RequiredFieldValidator>
    <asp:Label ID="QLabel6" runat="server" Text="Question 6"></asp:Label>
    <asp:RadioButtonList ID="RadioButtonList6" runat="server"
        RepeatDirection="Horizontal" RepeatLayout="Table">
        <asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
        <asp:ListItem Text="*Answer 2*" Value="Correct" />
        <asp:ListItem Text="*Answer 3*" Value="I" />
        <asp:ListItem Text="*Answer 4*" Value="4" />
    </asp:RadioButtonList>
        <asp:RequiredFieldValidator ID="Q6requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList6" ErrorMessage="Ensure question 6 is completed"></asp:RequiredFieldValidator>
    <asp:Label ID="QLabel7" runat="server" Text="Question 7"></asp:Label>
    <asp:RadioButtonList ID="RadioButtonList7" runat="server"
        RepeatDirection="Horizontal" RepeatLayout="Table">
        <asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
        <asp:ListItem Text="*Answer 2*" Value="Correct" />
        <asp:ListItem Text="*Answer 3*" Value="I" />
        <asp:ListItem Text="*Answer 4*" Value="4" />
    </asp:RadioButtonList>
        <asp:RequiredFieldValidator ID="Q7requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList7" ErrorMessage="Ensure question 7 is completed"></asp:RequiredFieldValidator>
    <asp:Label ID="QLabel8" runat="server" Text="Question 8"></asp:Label>
    <asp:RadioButtonList ID="RadioButtonList8" runat="server"
        RepeatDirection="Horizontal" RepeatLayout="Table">
        <asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
        <asp:ListItem Text="*Answer 2*" Value="Correct" />
        <asp:ListItem Text="*Answer 3*" Value="I" />
        <asp:ListItem Text="*Answer 4*" Value="4" />
    </asp:RadioButtonList>
        <asp:RequiredFieldValidator ID="Q8requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList8" ErrorMessage="Ensure question 8 is completed"></asp:RequiredFieldValidator>
    <asp:Label ID="QLabel9" runat="server" Text="Question 9"></asp:Label>
    <asp:RadioButtonList ID="RadioButtonList9" runat="server"
        RepeatDirection="Horizontal" RepeatLayout="Table">
        <asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
        <asp:ListItem Text="*Answer 2*" Value="Correct" />
        <asp:ListItem Text="*Answer 3*" Value="I" />
        <asp:ListItem Text="*Answer 4*" Value="4" />
    </asp:RadioButtonList>
        <asp:RequiredFieldValidator ID="Q9requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList9" ErrorMessage="Ensure question 9 is completed"></asp:RequiredFieldValidator>
    <asp:Label ID="QLabel10" runat="server" Text="Question 10" ></asp:Label>
    <asp:RadioButtonList ID="RadioButtonList10" runat="server"
        RepeatDirection="Horizontal" RepeatLayout="Table">
        <asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
        <asp:ListItem Text="*Answer 2*" Value="Correct" />
        <asp:ListItem Text="*Answer 3*" Value="I" />
        <asp:ListItem Text="*Answer 4*" Value="4" />
    </asp:RadioButtonList>
    <asp:RequiredFieldValidator ID="Q10requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList10" ErrorMessage="Ensure question 10 is completed"></asp:RequiredFieldValidator>
    <asp:Button ID="Button1" runat="server" Text="Submit Final Answers" OnClick="Submit_Click" Visible="true" />
    <script runat="server">
        protected void Submit_Click(object sender, EventArgs e)
        {
            int score = 0;
            List<RadioButtonList> list = new List<RadioButtonList>() { RadioButtonList1, RadioButtonList2, RadioButtonList3, RadioButtonList4, RadioButtonList5, RadioButtonList6, RadioButtonList7, RadioButtonList8, RadioButtonList9, RadioButtonList10 };
            foreach (var element in list)
            {
                if (element.SelectedValue == "Correct")
                {
                    score++;
                }

            }
            Response.Write("you scored: "+score);
            Button1.Visible = false;
        }
    </script>
</asp:Content>

Sorry for the state of this but im quite new and im trying my best to learn and code at the same time.

Daniel Turville
  • 87
  • 1
  • 10

1 Answers1

2

You could use Data Control such as ListView, GridView. Then you won't have to change a single line of code even if you have 100 questions.

enter image description here

<asp:ListView runat="server" ID="ListView1">
    <ItemTemplate>
        <%# Eval("Question") %>
        <asp:HiddenField runat="server" ID="IdHiddenField" Value='<%# Eval("Id") %>'/>
        <asp:RadioButtonList ID="AnswerRadioButtonList" runat="server"
            RepeatDirection="Horizontal" RepeatLayout="Table">
            <asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
            <asp:ListItem Text="*Answer 2*" Value="Correct" />
            <asp:ListItem Text="*Answer 3*" Value="I" />
            <asp:ListItem Text="*Answer 4*" Value="4" />
        </asp:RadioButtonList>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
            Display="Dynamic" ControlToValidate="AnswerRadioButtonList"
            ErrorMessage="Ensure question is completed" CssClass="field-validation-error" />
        <hr />
    </ItemTemplate>
</asp:ListView>
<asp:Button ID="Button1" runat="server" Text="Submit Final Answers"
    OnClick="Submit_Click" Visible="true" />

Code Behind

public class Quiz
{
    public int Id { get; set; }
    public string Question { get; set; }
    public string Answer { get; set; }
}

public partial class WebForm2 : System.Web.UI.Page
{
    // This could come from database
    private IList<Quiz> Quizzes
    {
        get
        {
            return new List<Quiz>
            {
                new Quiz {Id = 1, Question = "Question 1"},
                new Quiz {Id = 2, Question = "Question 2"},
                new Quiz {Id = 3, Question = "Question 3"},
                new Quiz {Id = 4, Question = "Question 4"},
                new Quiz {Id = 5, Question = "Question 5"}
            };
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListView1.DataSource = Quizzes;
            ListView1.DataBind();
        }
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        IList<Quiz> quizzes = new List<Quiz>();
        for (int i = 0, length = Quizzes.Count; i < length; i++)
        {
            ListViewDataItem item = ListView1.Items[i];
            var radioButtonList = item.FindControl("AnswerRadioButtonList") as RadioButtonList;
            var idHiddenField = item.FindControl("IdHiddenField") as HiddenField;
            quizzes.Add(new Quiz
            {
                Id = Convert.ToInt32(idHiddenField.Value),
                Answer = radioButtonList.SelectedValue
            });
        }
    }
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • Awesome thank you for the response, if i was to do it like this, how will I change the question and answers for each question so that they are different? – Daniel Turville Apr 06 '17 at 10:46
  • I keep getting an error with ListView1 not existing in current context? – Daniel Turville Apr 06 '17 at 11:17
  • You might want to read [Visual Studio Not Connecting Designer to Code Behind](http://stackoverflow.com/questions/20054823/visual-studio-not-connecting-designer-to-code-behind/20054951#20054951). – Win Apr 06 '17 at 13:42