arbeiten am Erstellen einer Web-basierten Revisionsressource und ich versuche, ein Multiple-Choice-Quiz in C# mit asp.net zu erstellen. Ich habe es erstellt und habe es funktioniert, aber es ist sehr lang und fragte mich, ob jemand eine Idee davon hatte, wie ich den Code kompakter machen könnte, auch wenn es das Endergebnis druckt es erscheint über meiner Navigationsleiste auf meiner Website von ich habe keine Idee warum.C# Multiple-Choice-Test
zusätzlich können die Antworten geändert werden und die Schaltfläche zum Absenden nur angeklickt, ich kann nicht für die Liebe von irgendetwas nichts finden, wie man dies stoppt.
Ich bin extrem neu und versuche mein Bestes. wenn mir jemand in die richtige Richtung zeigen könnte oder etwas vorschlagen könnte, wäre das toll.
Dank
ich für die Länge des Codes appologise:
<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>
Stattdessen können Sie versuchen, die Repeater-Kontrolle oder eine andere Bindungskontrolle zu verwenden, die sich entsprechend der Anzahl der Datensätze wiederholt, die Sie darin binden. –
Sie sollten sich mit der Verwendung von Methoden vertraut machen. Sie müssen Logik nicht wie Ihre Bewertung mehrere Male schreiben. –
Ich stimme @KevinShah über die Verwendung einer Repeater-Steuerelement und Datenbindung. Es wäre prägnanter. Außerdem besteht das Problem, wie Sie dies jetzt haben, darin, dass Sie den Code ändern müssen, wenn Sie das Quiz ändern möchten. Wenn Sie Datenbindung und Repeater verwenden, können Sie stattdessen einfach die Daten ändern. – EJoshuaS