如何使用 RadioButtonList 中的 Repeater 来重复 RadioButtonList 的 ListItem
How to use Repeater inside RadioButtonList to repeat ListItem of RadioButtonList
第一,中继器with在中继器里能用吗?
如果是,我将如何在以下场景中使用嵌套中继器。
<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中使用另一个repeater来重复每个问题的选项。
我想这样摆出来
很遗憾,您无法在 asp:RadioButtonList
内使用转发器。它只允许 ListItem
进入。你会得到一个错误,repeater 是一个未知元素。但是你可以在后面的代码中绑定asp:RadioButtonList
。
扩展 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));
}
}
第一,中继器with在中继器里能用吗? 如果是,我将如何在以下场景中使用嵌套中继器。
<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中使用另一个repeater来重复每个问题的选项。 我想这样摆出来
很遗憾,您无法在 asp:RadioButtonList
内使用转发器。它只允许 ListItem
进入。你会得到一个错误,repeater 是一个未知元素。但是你可以在后面的代码中绑定asp:RadioButtonList
。
扩展 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));
}
}