2017-04-06 117 views
1

我正在嘗試創建一個選擇題測驗,用戶將回答問題並在提交時提供一個分數,如果他們沒有回答其中的一個,我將使用必需的字段驗證器來顯示一條消息即使我沒有完成提交時的所有問題,系統仍然會計算他們的分數。 我需要能夠停止提交,直到所有的單選按鈕列表完成,但我不知道如何做到這一點。 我的代碼是在這裏:如果RequiredFieldValidator失敗,則阻止提交

<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> 

之前點擊提交:

enter image description here

我的提交按鈕自敗喜歡它:

enter image description here

點擊提交時沒有答案被填充後應該但我想能夠阻止它被提交和sco重新輸出,直到所有問題得到解答。

所有這些都是新的,所以很抱歉,如果這看起來很容易或愚蠢。 感謝

回答

1

嘗試將enableclientscript="True"屬性:

<asp:RequiredFieldValidator ID="Q10requiredvalidator" 
          runat="server" Display="Dynamic" 
          ControlToValidate="RadioButtonList10" 
          CssClass="text-danger" 
          ErrorMessage="Ensure question 10 is completed" 
          enableclientscript="True"> 
</asp:RequiredFieldValidator> 

然後用this.IsValid防止按鈕的代碼,如果驗證失敗:

protected void Submit_Click(object sender, EventArgs e) 
{ 
    if (!this.IsValid) 
     return; 

    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; 
} 
+0

曾爲完美,謝謝你們的時間。 –

+0

@DanielTurville樂意幫忙! –