Reputation: 87
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>
Upvotes: 2
Views: 2155
Reputation: 4622
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:
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.
Upvotes: 0
Reputation: 1347
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);
}
Upvotes: 1
Reputation: 1769
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++;
}
}
Upvotes: 1