2016-11-14 12 views
2

最初に、リピータで使用できるリピータは使用できますか? もしそうなら、以下のシナリオでどのようにネストされたリピーターを使うことができますか?RadioButtonList内のRepeaterを使用してRadioButtonListのListItemを繰り返す方法

<div class="row"> 
    <asp:Repeater ID="rp_Question" runat="server"> 
     <ItemTemplate> 
      <p class="_100"> 
       <h2 id="h4_Question" runat="server"><%# Eval("question_text") %></h2> 
      </p> 
      <p class="left"> 
       <asp:RadioButtonList ID="rb_Question" runat="server"> 
        <asp:ListItem Text="Option1" Value="1"></asp:ListItem> 
        <asp:ListItem Text="Option2" Value="2"></asp:ListItem> 
        <asp:ListItem Text="Option3" Value="3"></asp:ListItem> 
        <asp:ListItem Text="Option4" Value="4"></asp:ListItem> 
       </asp:RadioButtonList> 
      </p> 
     </ItemTemplate> 
    </asp:Repeater 

各質問のオプションはデータベースに保存され、最小限のオプションが3可能性があり、最大値は6どのように私は内の他のリピータを使用することができる可能性があり

rp_Question.DataSource = _question.GetAll(); 
rp_Question.DataBind(); 

をバインドリピータrp_Question各質問のオプションを繰り返します。 このように置いて見せたいです。

enter image description here

答えて

3

KateCuteの回答では、ItemDataBoundイベントを使用できます。

<asp:Repeater ID="rp_Question" runat="server" OnItemDataBound="rp_Question_ItemDataBound"> 

コードビハインドです。

protected void rp_Question_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    //find the radiobuttonlist with findcontrol and cast back to it's original type 
    RadioButtonList rb_Question = e.Item.FindControl("rb_Question") as RadioButtonList; 

    //get the current datarow 
    DataRowView row = e.Item.DataItem as DataRowView; 

    //get the id from the datarow object 
    string questionID = row["question_id"].ToString(); 

    //get the answers from the db with questionID and bind them as listitems just like in the loop below 

    //just a loop to add some listitems for demo 
    for (int i = 0; i < 5; i++) 
    { 
     rb_Question.Items.Insert(i, new ListItem("Option " + i.ToString(), i.ToString(), true)); 
    } 
} 
3

残念ながら、あなたはasp:RadioButtonList内のリピータを使用することはできません。それは内側にのみListItemが可能です。リピーターは知られていない要素であるというエラーが表示されます。しかし、コードの後ろにasp:RadioButtonListを束縛することができます。

+0

あなたはリピータ内に配置された 'asp:RadioButtonList'をバインドするコードを教えてください。 –

関連する問題