2017-03-22 14 views
2

Webベースのリビジョンリソースの作成に取り組んでいます。私はasp.netでC#で複数選択のクイズを作成しようとしています。 私はそれを作ってそれを稼働させましたが、それは非常に長く、どのようにしてコードをコンパクトにすることができるかというアイデアがあるのだろうかと疑問に思っていたのですが、 最終スコアを印刷するときにも、なぜ私は考えていない。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> 
+1

これに代わって、リピータコントロールやその他のバインディングコントロールを使用して試すことができます。このコントロールは、バインドしたレコードの数だけ繰り返されます。 –

+0

メソッドを使い慣れている必要があります。評価のようなロジックを複数回書く必要はありません。 –

+1

リピータコントロールとデータバインディングの使用について@KevinShahに同意します。より簡潔になるでしょう。また、あなたがこれを持っている方法の問題は、クイズを変更する必要がある場合、コードを変更する必要があるということです。データバインディングとリピーターを使用すると、代わりにデータを変更できます。 – EJoshuaS

答えて

1

Submit_Click方法について

RadioButtonList[] lists = new RadioButtonList[] {RadioButtonList1, RadioButtonList2, RadioButtonList3, RadioButtonList4, RadioButtonList5, RadioButtonList6, RadioButtonList7, RadioButtonList8, RadioButtonList9, RadioButtonList10 }; 

foreach (RadioButtonList element in lists) { 
      if (element.SelectedValue == "correct") { 
       Score++; 
     } 
} 
1

を、私はことをお勧めします何らかの並べ替えのListまたはEnumerableのすべてのCheckBoxを持つ次に、このリストを反復し、各反復で結果を評価することができます。行で何か:私は厳密解を持っていないが、あなたは正しい方向に指摘さについて尋ねられたので、

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

これは本当にうれしいですが、これにスクリプトコードを変更すると、出力のスコアは0になります。素早い返信をありがとう –

+0

@DanielTurville、私はちょうど "正解"の代わりに "正しい"と入力したことに気付きました。それを「正解」に変更すれば、おそらくあなたはいいですね。さらに、string.Equalsメソッドを使用して比較することもできます。 Ex。 'string.Equals(" correct "、要素。SelectedValue、StringComparison.OrdinalIgnoreCase)。それは値を比較する際に無視されます。 –

0

、ここにあなたが取るべき手順は次のとおりです。

  • ユーザーコントロールを作成します。質問文用のTextBoxとラジオコントロールリストまたは選択肢用のものがあります。識別子や質問を正しく識別するための隠しフィールドを追加します。
  • メインWebページでは、Repeaterコントロールを使用するか、ユーザーコントロールにデータを動的に追加します。
  • 正しい選択肢の選択を確認するためのすべてのコードは、ユーザーコントロールにある必要があります。
  • メインフォームでは、送信時に各ユーザーコントロールにアクセスし、結果を収集します。

は、私が数年前にASP.NET Webフォームで同様のアプリを作ったが、私は持っていた「< <戻る」と「次へ>>」ボタンを1つのページに一つだけ質問が表示されていましたし、全体のアプリはそうする時間を記録してテスト期間が経過すると、ユーザーは最終ページに移動しました。

+0

私はこれが私が知っていることよりも少し先であると思っています。私があなたが何を意味しているかの例を学ぶか見ているところに私をリンクさせることができますか?ありがとう –

+0

確かに、私たちはこの議論をstackoverflowから取ることができますか? –

+0

あなたが好きなものは、確かな男ですか? –