在asp.net的radiobuttonlist中的转发器

时间:2014-04-23 16:50:50

标签: c# asp.net radiobuttonlist

嗨我需要在asp.net中设计像这个图像的代码:

enter image description here

列表来自数据库,元素也来自数据库。

但是我需要制作每个盒子:(1个列表 - 多个元素)是一个分开的radiobox 所以我可以点击框内的任何位置并检查其中的收音机

我想创建一个asp:RadioButtonList作为主数据源(用于列表)

 <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                </asp:RadioButtonList>  

在每个收音机里面为每个列表中的元素制作一个转发器...怎么做? 如何在radiobuttonlist中添加转发器?

我试过这段代码

  <asp:RadioButtonList ID="radio" runat="server" DataSourceID="radio" DataTextField="name" DataValueField="id">
                            <asp:HiddenField runat="server" ID="hidden" value='<%#Eval("id") %>'>
                            </asp:HiddenField>
                            <asp:Repeater runat="server" ID="repeater" DataSourceID="repeaterds">
                                <ItemTemplate>
                                    <%#Eval("name") %>
                                </ItemTemplate>
                            </asp:Repeater>
                            <asp:SqlDataSource ID="repeaterds" runat="server" ConnectionString="<%$ ConnectionStrings:samy_sarc %>" SelectCommand="select * from level where (team_id = @id)">
                                <SelectParameters>
                                <asp:ControlParameter ControlID="hidden" Name="@id" PropertyName="Value" />
                            </SelectParameters>


                            </asp:SqlDataSource>
                        </asp:RadioButtonList>
                         <asp:SqlDataSource ID="radiods" runat="server" ConnectionString="<%$ ConnectionStrings:samy_sarc %>" SelectCommand="select * from team"></asp:SqlDataSource>

但它不起作用,因为radiobuttonlist没有采用兄弟元素

1 个答案:

答案 0 :(得分:1)

也许我误解了你的问题,不需要使用转发器......

public class TData
{
    public String TText { get; set; }

    public UInt32 TValue { get; set; }
}

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<TData> Data = new List<TData>
        {
            new TData{TText = "JSON", TValue = 0},
            new TData{TText = "C#", TValue = 1},
            new TData{TText = "JAVA", TValue = 2},
        };

        this.RadioButtonList1.DataTextField = "TText";
        this.RadioButtonList1.DataValueField = "TValue";
        this.RadioButtonList1.DataSource = Data;
        this.RadioButtonList1.DataBind();
    }
}
好吧,让我们改进吧

asp.net page

    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <asp:RadioButton runat="server" Text="<%# (Container.DataItem as Dummy.TData).TText %>" />
            <asp:Repeater runat="server" DataSource="<%# (Container.DataItem as Dummy.TData).TValue %>">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text="<%# (Container.DataItem as Dummy.TData1).TText %>"></asp:Label>
                 </ItemTemplate>
             </asp:Repeater>
        </ItemTemplate>
    </asp:Repeater>

和代码背后:

public class TData1
{
    public String TText { get; set; }

    public String TValue { get; set; }
}

public class TData
{
    public String TText { get; set; }

    public List<TData1> TValue { get; set; }
}

 protected void Page_Load(object sender, EventArgs e)
    {
        List<TData1> lst = new List<TData1> 
        {
            new TData1 {TText = "JSON", TValue = "0"},
            new TData1 {TText = "C#", TValue = "1"},
            new TData1 {TText = "JAVA", TValue = "1"}
        };

        List<TData> Data = new List<TData>
        {
            new TData{TText = "JSON", TValue = lst},
            new TData{TText = "C#", TValue = lst},
            new TData{TText = "JAVA", TValue = lst}
        };

        this.Repeater1.DataSource = Data;
        this.Repeater1.DataBind();
    }

已经完成了

enter image description here