【问题标题】:Populating an ASP.NET Repeater填充 ASP.NET 中继器
【发布时间】:2013-02-17 20:20:13
【问题描述】:

我的 aspx 中有一个中继器:

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

在网页的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();
 }

但在尝试时,它什么也没显示。 我做错了什么?

【问题讨论】:

  • 发布转发器的aspx代码,获取数据的代码,在哪里调用这个方法?
  • 您将单选按钮列表绑定到转发器而不是数据。如果您将单选按钮放在中继器 itemTemplate 中并仅绑定数据,您应该能够得到它
  • 与论坛网站不同,我们不使用“谢谢”、“任何帮助表示赞赏”或Stack Overflow 上的签名。请参阅“Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.

标签: c# asp.net radio-button


【解决方案1】:

试试这个:

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();
    }
}

你看,每个IGrouping&lt;string, Tuple&lt;string, string&gt;&gt; 指的是某个GroupName 的一组RadioButton,它们也是repeater 中的item。我们为每个项目创建一个新的 RadioButtonList,它代表整个 RadioButtons 组。

您可以通过使用与 Tuple 不同的 DataStructure 使其更好,通常不清楚 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;
    }
}

【讨论】:

  • 太棒了!有用!现在,我怎样才能“接收”所有选定的?
  • 是的,您必须遍历中继器项才能找到每个 RadioButtonList。
  • @Javi Dorfsman,我几乎在每一步都更新了答案,请修改您的代码。最后,我添加了一个如何获取所选值的示例。
  • 你太棒了!谢谢!
  • @Mt.Schneiders,我已经尝试过您提出的迭代,但它不起作用。我得到字符串长度为 0 :/
【解决方案2】:

您应该将RadioButton 放入转发器并将其绑定到createRadioButtons 事件中。

【讨论】:

  • 能否详细一点?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多