[英]Create multiple RadioButtonLists in ASP.NET from a single table in SQL Server using C#
I would like to use ASP.net, C# and SQL to display a list of games with radio buttons like below (the x is the radio). 我想使用ASP.net,C#和SQL来显示带有下面的单选按钮的游戏列表(x是收音机)。 One team can be selected for each game. 每个游戏都可以选择一个团队。
game 1: x team 4 x team 2 游戏1:x团队4 x团队2
game 2: x team 6 x team 1 游戏2:x团队6 x团队1
game 3: x team 5 x team 3 游戏3:x团队5 x团队3
The game list is stored in a table in an SQL database. 游戏列表存储在SQL数据库的表中。 So far, I can pull all teams into one big RadioButtonList. 到目前为止,我可以将所有团队拉成一个大的RadioButtonList。 I cannot figure out how to create multiple RadioButtonList controls from this single table of games. 我无法弄清楚如何从这单个游戏表创建多个RadioButtonList控件。 Does anyone know how this can be accomplished - or reference to an example / tutorial that accomplishes something like this? 有谁知道如何做到这一点-或参考一个例子或教程完成这样的事情?
Use a listview for the different games and radiobuttonlist for the items 使用列表视图查看项目的不同游戏和radiobuttonlist
like such 像这样
<asp:ListView ID="ListView1" runat="server" onitemdatabound="ListView1_ItemDataBound">
<ItemTemplate>
<asp:Label runat="server" ID="txtGame" Text='<%# Bind("GameName") %>'></asp:Label><br />
<asp:HiddenField ID="hdnGameID" runat="server" Value='<%# Bind("GameID") %>'/>
<asp:RadioButtonList runat="server" ID="rblTeam" DataTextField="TeamName" DataValueField="TeamID">
</asp:RadioButtonList>
</ItemTemplate>
</asp:ListView>
then on your code behind 然后你的代码背后
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var oGame = from g in myDB.Game
group g by g.GameName into result
select new { GameID = result.Key, GameName = result };
ListView1.DataSource = oGame;
ListView1.DataBind();
}
}
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
HiddenField hdnGameID = (HiddenField)e.Item.FindControl("hdnGameID");
RadioButtonList rblTeam = (RadioButtonList)e.Item.FindControl("rblTeam");
var oTeam = from t in myDB.Game
where t.GameID == hdnGameID.Value
select t;
rblTeam.DataSource = oTeam;
rblTeam.DataBind();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.