简体   繁体   English

使用C#从SQL Server中的单个表在ASP.NET中创建多个RadioButtonLists

[英]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.

相关问题 ASP.NET C#在SQL Server数据库表中搜索 - ASP.NET C# Search in a SQL Server Database Table ASP.NET MVC / C#/ SQL Server应用程序:多个用户同时更新表的问题 - ASP.NET MVC / C# / SQL Server Application : Issue with multiple users updating a table same time 使用C#将SQL Server 2008中的多个SQL查询合并到ASP.NET上的数据表中 - Combine multiple SQL queries in SQL Server 2008 into a datatable on ASP.NET using C# 如何从Asp.Net/C# Codebehind中的单个SQL Select语句返回多个字符串变量? - How to return multiple string variables from a single SQL Select Statement in Asp.Net/C# Codebehind? 从SQL Server(C#/ ASP.Net)在Site.Master中使用多条件搜索栏/按钮时遇到问题 - Having trouble using a multiple criteria search bar/button in Site.Master from an SQL server (C# / ASP.Net) 如何在ASP.NET C#中使用PrincipleSearcher在单个搜索中包含来自AD帐户的多个OU - How to include multiple OU from AD account in a single search using PrincipleSearcher in asp.net c# 使用asp.net在运行时创建SQL Server表 - Create SQL Server table at runtime using asp.net 如何使用ASP.NET从C#中的SQL Server表中自动完成TextBox中的值 - How can I autocomplete values in a TextBox from SQL Server table in C# with ASP.NET 如何在asp.net c#中从SQL Server数据库表中移动下一条/上一条记录 - How to move next/previous record from SQL Server database table in asp.net c# 如何在C#ASP.Net中将表格数据从Excel上传到SQL Server 2008 - How to upload table data from Excel to SQL Server 2008 in C# ASP.Net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM