2

working on creating a web based revision resource and i am trying to create a multiple choice quiz in c# with asp.net. I have created it and got it working but it is very long and was wondering if anyone had an idea of how i could make the code more compact, also when it prints the final score it appears above my nav bar on my website by i have no idea why.

in addition, the answers can be changed and the submit button just re clicked, i cannot for the love of anything find anything on how to stop this.

I am extremely new to everything and am trying my best. if someone could point me in the right direction or suggest something that might help that would be awesome.

Thanks

I appologise for the length of the code:

<asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Body" runat="server">
    <asp:Label ID="Label1" runat="server" Text=""></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>
    <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>
    <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>
    <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>
    <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: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: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: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: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: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:Button ID="Button1" runat="server" Text="Submit Final Answers" OnClick="Submit_Click" />
    <script runat="server">
        protected void Submit_Click(object sender, EventArgs e)
        {
            int Score = 0;
            string selectedValue1 = RadioButtonList1.SelectedValue;
            if (selectedValue1 == "Correct")
            {
                Score++;
            }
            string selectedValue2 = RadioButtonList2.SelectedValue;
            if (selectedValue2 == "Correct")
            {
                Score++;
            }
            string selectedValue3 = RadioButtonList3.SelectedValue;
            if (selectedValue3 == "Correct")
            {
                Score++;
            }
            string selectedValue4 = RadioButtonList4.SelectedValue;
            if (selectedValue4 == "Correct")
            {
                Score++;
            }
            string selectedValue5 = RadioButtonList5.SelectedValue;
            if (selectedValue5 == "Correct")
            {
                Score++;
            }
            string selectedValue6 = RadioButtonList6.SelectedValue;
            if (selectedValue6 == "Correct")
            {
                Score++;
            }
            string selectedValue7 = RadioButtonList7.SelectedValue;
            if (selectedValue7 == "Correct")
            {
                Score++;
            }
            string selectedValue8 = RadioButtonList8.SelectedValue;
            if (selectedValue8 == "Correct")
            {
                Score++;
            }
            string selectedValue9 = RadioButtonList9.SelectedValue;
            if (selectedValue9 == "Correct")
            {
                Score++;
            }
            string selectedValue10 = RadioButtonList10.SelectedValue;
            if (selectedValue10 == "Correct")
            {
                Score++;
            }
            Response.Write(Score);

        }
    </script>
</asp:Content>
3
  • 1
    instead of this you can try using repeater control or some other binding control which will repeat as per the number of records you bind in that. Commented Mar 22, 2017 at 12:50
  • You should get familiar with using methods. You don't have to write logic like your evaluation multiple times. Commented Mar 22, 2017 at 12:51
  • 1
    I agree with @KevinShah about using a repeater control and data binding. It would be more concise. Also, the problem with how you have this now is that if you want to change the quiz you have to change the code - if you use data binding and repeaters you could just change the data instead. Commented Mar 22, 2017 at 13:08

3 Answers 3

1

This may shorten up the evaluation phase:

RadioButtonList[] lists = new RadioButtonList[] {RadioButtonList1, RadioButtonList2, RadioButtonList3, RadioButtonList4, RadioButtonList5, RadioButtonList6, RadioButtonList7, RadioButtonList8, RadioButtonList9, RadioButtonList10 };

foreach (RadioButtonList element in lists ) {
            if (element.SelectedValue == "correct") {
                Score++;
        }
}
Sign up to request clarification or add additional context in comments.

Comments

1

For the Submit_Click method, I would encourage you to have all your CheckBoxes in a List or Enumerable of some sort. Then you could iterate on this List and evaluate the Result in each iteration. Something in the lines of:

protected void Submit_Click(object sender, EventArgs e)
{
     int score = 0;
     List<RadioButtonList> list = new List<RadioButtonList>() { RadioButtonList1, RadioButtonList2, ... RadioButtonList3 };
     foreach (var element in list) 
     {
        if (element.SelectedValue == "correct") {
            score++;
     }
     Response.Write(score);
}

2 Comments

This is really good thankyou, however i only get an output score of 0 no matter the input when changing the script code to this. Thanks for the swift reply
@DanielTurville, I just realized I typed "correct" instead of "Correct". Change it to "Correct" and you're probably good to go. Additionaly you could compare it using string.Equals method. Ex. `string.Equals("correct", element.SelectedValue, StringComparison.OrdinalIgnoreCase). That would ignore the case when comparing the values.
0

I do not have an exact solution but since you asked about being pointed to the right direction, here are the steps you should take:

  • Create a User Control with a TextBox for question statement and a radio control list or anything for the choices, Also add a hidden field to put an identifier or something that identifies the question correctly.
  • On the main webpage either you use a Repeater control or Add the User Control dynamically with the data.
  • All the code for checking the correct Choice selection should be in the user control.
  • In the main form, when it is submitted, access each user control and collect the result.

I made a similar app in ASP.NET webForms years ago but I had "<< Back" and "Next >>" buttons so on one page only one question was displayed and the whole app recorded time so when the test duration elapses it took the user to the end page.

4 Comments

I think this is a little ahead of what i know, would you be able to link me to where i can learn or see an example of what you mean. Thanks
Sure, is there someway we can take this discussion off stackoverflow?
skype pehaps? or email me at [email protected]
skype is good for me, 'turban742' is my username. my picture is a skull

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.