2012-01-04 2 views
0

私はasp.netで2つのクイズを作ろうとしています。 ラジオボタンを使用してmcqの選択肢rを表示します。コードの後ろに、私はradiobuttonがチェックされているかどうかをチェックするときに、そのif文が実行されないようにします。 ASPXコード:背後ラジオボタンがチェックされているかどうかチェックしようとすると、コードは実行されません

<ItemTemplate> 
      <asp:Literal ID="Literal1" runat="server" Text='<%#Eval("ques") %>'></asp:Literal><br /> 
      <asp:RadioButton GroupName="a" ID="RadioButton1" Text='<%#Eval("ch1") %>' runat="server" /><br /> 
      <asp:RadioButton GroupName="a" ID="RadioButton2" Text='<%#Eval("ch2") %>' runat="server" /><br /> 
      <asp:RadioButton GroupName="a" ID="RadioButton3" Text='<%#Eval("ch3") %>' runat="server" /><br /> 
      <asp:RadioButton GroupName="a" ID="RadioButton4" Text='<%#Eval("ch4") %>' runat="server" /><br /> 

      <asp:Label ID="Label1" runat="server" Text='<%#Eval("ans") %>' Visible="false"></asp:Label><br /> 
      <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br /> 
     </ItemTemplate> 

コード:

protected void Button1_Click(object sender, EventArgs e) 
    { 
     int count = 0; 

     foreach(RepeaterItem Items in Repeater1.Items) 
     { 

      RadioButton r1 = (RadioButton)Items.FindControl("RadioButton1"); 
      RadioButton r2 = (RadioButton)Items.FindControl("RadioButton2"); 
      RadioButton r3 = (RadioButton)Items.FindControl("RadioButton3"); 
      RadioButton r4 = (RadioButton)Items.FindControl("RadioButton4"); 
      Label l3 = (Label)Items.FindControl("Label3"); 

      Label l=(Label)Items.FindControl("Label1"); 
      l3.Text = "hello?"; 
      if (r1.Checked) 
      { 
       if(r1.Text==l.Text) 
        count++; 
      } 
      else 
      { 
       if (r2.Checked) 
       { 
        if(r2.Text==l.Text) 
         count++; 
       } 
      } 
       // and so on for all 4 options 
     } 
     Label2.Visible = true; 
     Label2.Text = "your score is " + count;  //always zero! 

    } 

答えて

1

デバッガをステップし、あなたのラインしている場合

if(r1.Text==l.Text) 
    count++; 

はその後、私はラインif (r1.Checked)がfalseに評価されていることを推測する、実行されていません。

このページのPage_Load()メソッドでは、これらのラジオボタンのデータバインディングや操作はありますか?その場合は、if(!Page.IsPostBack){ ... }という条件でそれらをラップしない限り、ユーザーがラジオボタンに行った操作はすべて消去されるため、r1.Checkedfalseとなります。

私はそれが助けてくれることを祈っています:)幸運。

0

あなたは次のことを行う必要があります:

trueにラジオボタンコントロールに AutoPostBackを設定
  1. 少なくとも背後にあるコード内のメソッドをトリガーするprotected void Button1_Click(object sender, RepeaterCommandEventArgs e)

に「のButton1 Click」

  • 変更のButton1メソッドのシグネチャにRepeaterコントロールにOnItemCommandを設定します。

  • 関連する問題