![](/img/trans.png)
[英]Unable to populate RadioButtonList inside ASP.NET Repeater Template
[英]repeater inside a radiobuttonlist in asp.net
嗨,我需要在asp.net中设计类似此图像的代码:
列表来自数据库,元素也来自数据库。
但是我需要制作每个框:(1个列表-多个元素)是一个单独的单选框,因此我可以单击该框内的任意位置并检查其中的单选框
我考虑为主数据源创建一个asp:RadioButtonList(用于列表)
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
</asp: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>
但这是行不通的,因为单选按钮列表没有包含同级元素
也许我误解了您的问题,不需要使用中继器...
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页面
<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();
}
完成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.