2016-11-14 41 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 

直放站綁定

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

每題的選項保存在數據庫中,最小的選擇可能是3和最大的可能是6.如何使用其它中繼器內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'放置在中繼器內嗎? –

相關問題