正在創建一個基於web的修訂資源,我正嘗試在c#中用asp.net創建一個選擇題測驗。 我已經創建並獲得它的工作,但它很長,並想知道如果有人有我想如何使代碼更緊湊的想法, 也打印最後的分數,它出現在我的網站上我的導航欄上方我不知道爲什麼。c#選擇題測試
此外,答案可以改變,提交按鈕只是點擊,我不能爲任何事情的愛找到任何關於如何阻止這一點。
我對所有事情都非常陌生,正在盡我所能。如果有人能指引我正確的方向或建議可能會有幫助的東西會很棒。
感謝
我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>
代替這個,你可以嘗試使用中繼器控制或其他綁定控件,它將根據你綁定的記錄數重複。 –
您應該熟悉使用方法。您不必多次像評估一樣編寫邏輯。 –
我同意@KevinShah關於使用中繼器控制和數據綁定。它會更簡潔。此外,現在的問題是,如果您想更改測驗,則必須更改代碼 - 如果使用數據綁定和中繼器,則可以改爲更改數據。 – EJoshuaS