禁止提交,除非所有字段都已填写asp.net和c#(选择题测验)
我正在尝试创建一个选择题测验,用户将回答问题,并在提交时获得分数。如果他们没有回答其中一个问题,我将使用所需的字段验证器显示消息,然而,即使我在点击提交时没有完成所有的问题,系统仍然会计算他们的分数。 我需要能够停止提交,直到所有单选按钮列表完成,但我不知道如何做到这一点。 我的代码在这里:禁止提交,除非所有字段都已填写asp.net和c#(选择题测验),c#,asp.net,C#,Asp.net,我正在尝试创建一个选择题测验,用户将回答问题,并在提交时获得分数。如果他们没有回答其中一个问题,我将使用所需的字段验证器显示消息,然而,即使我在点击提交时没有完成所有的问题,系统仍然会计算他们的分数。 我需要能够停止提交,直到所有单选按钮列表完成,但我不知道如何做到这一点。 我的代码在这里: <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <asp:Label
<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" CssClass="text-danger" 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" CssClass="text-danger" 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" CssClass="text-danger" 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" CssClass="text-danger" ErrorMessage="Ensure question 4 is completed"></asp:RequiredFieldValidator>
</div>
<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>
<div>
<asp:RequiredFieldValidator ID="Q5requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList5" CssClass="text-danger" ErrorMessage="Ensure question 5 is completed"></asp:RequiredFieldValidator>
</div>
<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>
<div>
<asp:RequiredFieldValidator ID="Q6requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList6" CssClass="text-danger" ErrorMessage="Ensure question 6 is completed"></asp:RequiredFieldValidator>
</div>
<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>
<div>
<asp:RequiredFieldValidator ID="Q7requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList7" CssClass="text-danger" ErrorMessage="Ensure question 7 is completed"></asp:RequiredFieldValidator>
</div>
<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>
<div>
<asp:RequiredFieldValidator ID="Q8requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList8" CssClass="text-danger" ErrorMessage="Ensure question 8 is completed"></asp:RequiredFieldValidator>
</div>
<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>
<div>
<asp:RequiredFieldValidator ID="Q9requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList9" CssClass="text-danger" ErrorMessage="Ensure question 9 is completed"></asp:RequiredFieldValidator>
</div>
<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>
<div>
<asp:RequiredFieldValidator ID="Q10requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList10" CssClass="text-danger" ErrorMessage="Ensure question 10 is completed"></asp:RequiredFieldValidator>
</div>
<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>
在未填写答案时点击提交后:
我的submit按钮会像预期的那样消失,但我希望能够阻止它被提交并输出分数,直到所有问题都得到回答
对所有这些都是陌生的,如果这看起来简单或愚蠢,那么很抱歉。
谢谢我不认为你的代码中有问题,除非所有单选按钮都因为FieldValidator而未被选中,否则它将无法工作,或者你想要其他东西?@MAdeelKhalid,我添加了屏幕截图来说明我的意思