在 ASP.NET 中填充 Repeater 控件

5

我在我的aspx页面上有一个repeater:

<asp:Repeater ID="rptDummy" runat="server" OnItemDataBound="rptDummy_OnItemDataBound"
     Visible="true">
</asp:Repeater>

在 web 的 C# 部分,我编写了以下函数:
 protected void createRadioButtons(DataSet ds){
     List<System.Web.UI.WebControls.RadioButton> buttons = new List<System.Web.UI.WebControls.RadioButton>();
     foreach (DataTable dt in ds.Tables){
            foreach (DataRow r in dt.Rows){
               System.Web.UI.WebControls.RadioButton rb = new System.Web.UI.WebControls.RadioButton();
               rb.Text = r[1] + " " + r[2] + " " + r[3] + " " + r[4];
               rb.GroupName = (string)r[5];
               buttons.Add(rb);
            }
      }
      rptDummy.DataSource = buttons;
      rptDummy.DataBind();
 }

但是当我尝试运行它时,却没有任何输出。我做错了什么?

请发布Repeater的aspx代码,获取数据的代码以及您在哪里调用此方法的代码? - syed mohsin
1
你正在将一组单选按钮绑定到重复器而不是数据上。如果你将单选按钮放在重复器的itemTemplate中,只需绑定数据,就可以得到它。 - Andrew Walters
1
与论坛网站不同,我们在 [so] 上不使用“谢谢”、“感激任何帮助”或签名。请参阅“应该从帖子中删除‘Hi’、‘thanks’、标语和问候语吗? - John Saunders
2个回答

12

尝试这个:

1 - 定义 Repeater

<asp:Repeater ID="rptDummy" runat="server" OnItemDataBound="rptDummy_OnItemDataBound" >
    <ItemTemplate>
         <asp:RadioButtonList ID="rbl" runat="server" DataTextField="Item2" DataValueField="Item2" />
    </ItemTemplate>
</asp:Repeater>

2 - 构建数据结构并绑定重复器:

List<Tuple<string,string>> values = new List<Tuple<string,string>>();

foreach (DataTable dt in ds.Tables){
    foreach (DataRow r in dt.Rows){
       string text = r[1] + " " + r[2] + " " + r[3] + " " + r[4];
       string groupName = (string)r[5];
       values.Add(new Tuple<string,string>(groupName, text));
    }
}

//Group the values per RadioButton GroupName
rptDummy.DataSource = values.GroupBy(x => x.Item1);
rptDummy.DataBind();

3 - 定义 OnItemDataBound 事件:

protected void rptDummy_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        IGrouping<string, Tuple<string, string>> group = (IGrouping<string, Tuple<string, string>>)e.Item.DataItem;
        RadioButtonList list = (RadioButtonList)e.Item.FindControl("rbl");

        list.DataSource = group;
        list.DataBind();
    }
}

You see,每个IGrouping<string,Tuple<string,string>>都指代一个特定 GroupName 的 RadioButton 组,它们也是 Repeater 中的项。对于每个项,我们创建一个新的 RadioButtonList,表示整个 RadioButton 组。

您可以通过使用不同的数据结构而不是 Tuple 来使其更好,因为 Item1Item2 的含义通常不清楚。

更新:

如果您想查看所选值:

protected void button_OnClick(object sender, EventArgs e)
{
    foreach (RepeaterItem item in rptDummy.Items)
    {
        RadioButtonList list = (RadioButtonList)item.FindControl("rbl");
        string selectedValue = list.SelectedValue;
    }
}

太好了!它起作用了!现在,我怎样才能“接收”所有选定的内容呢? - Javi
是的,您需要遍历 Repeater 项以查找每个 RadioButtonList。 - Mateus Schneiders
@Javi Dorfsman,我在几乎每一步都进行了一些更改,更新了答案,请检查您的代码。最后,我添加了一个获取所选值的示例。 - Mateus Schneiders
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/25523/discussion-between-javi-dorfsman-and-mt-schneiders - Javi
如果是像这样的东西怎么办:http://stackoverflow.com/questions/31254126/how-to-generate-dynamic-labels-and-use-the-column-name-and-value-as-the-text - SearchForKnowledge
显示剩余3条评论

1
你应该将RadioButton放在重复器中,并在createRadioButtons事件中进行绑定。

请问您能否提供更详细的信息? - Javi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接