填充一个ASP.NET直放站 [英] Populating an ASP.NET Repeater

查看:205
本文介绍了填充一个ASP.NET直放站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中继器在我的aspx:

I have a repeater in my aspx:

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

在网络的C#的一面我写了这个功能:

in the c# side of the web I wrote this function:

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

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

But when trying it, it shows nothing. What am I doing wrong?

推荐答案

试试这个:

1 - 定义转发

1 - Define the 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 事件:

3 - Define the OnItemDataBound event:

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;字符串,元组LT;字符串,字符串&GT;&GT; 是指一组特定组名的单选按钮,它们也可从项目中继器。对于每个项目,我们创建一个新的单选按钮列表的重新presents单选按钮的整个集团。

You see, each IGrouping<string, Tuple<string, string>> refers to a group of RadioButtons of a certain GroupName, they are also the items from the repeater. For each item we create a new RadioButtonList that represents the whole group of RadioButtons.

您可以更好地通过使用不同的数据结构比元组,它往往是不清楚项目1 项目2 表示。

You can make it better by using a different DataStructure than a Tuple, it is often unclear what Item1 and Item2 means.

更新:

如果你想看到所选的值:

If you want to see the selected values:

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

这篇关于填充一个ASP.NET直放站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆