0

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). One team can be selected for each game.

game 1:   x team 4   x team 2
game 2:   x team 6   x team 1
game 3:   x team 5   x team 3

The game list is stored in a table in an SQL database. So far, I can pull all teams into one big RadioButtonList. I cannot figure out how to create multiple RadioButtonList controls from this single table of games. Does anyone know how this can be accomplished - or reference to an example / tutorial that accomplishes something like this?

1 Answer 1

2

Use a listview for the different games and radiobuttonlist for the items

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Seems like that should work, but still doesn't address how I get the games in the SQL table separated. I am new to all of this so hopefully I'm just missing something simple here...
Yes 1 table. I see you added some code, I'm checking it out now. It would need to load 2 teams per RadioButtonList, right?
Answer updated. I assumed that you use a table name Game so I used Game Table only and grouped the listview data by GameName

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.