How to retrieve a value from a set of radio buttons

R

Rob

Hi all,
I've got multiple sets of radio button that are dynamically created in
code and populated by a database query. The query returns about 20
recordsets with 3 radio buttons per recordset and I want to be able to
retrieve the selected value from each of the sets of radio buttons.The
names of each group of radio buttons is set by the database and I don't
know how to retrieve the values because I don't know the names of these
radio buttons.Is there any way of retrieving the values without going to
the database again?

Here's the code to populate the radio buttons:

Any help would be appreciated.
Thanks
Rob

Private Sub DisplayGames()
Dim rb1 As RadioButton
Dim rb2 As RadioButton
Dim rbTie As RadioButton
Dim tr As TableRow
Dim td1 As TableCell
Dim td2 As TableCell
Dim td3 As TableCell
Dim tdVimage As TableCell
Dim vImage As Image
Dim tdHimage As TableCell
Dim hImage As Image
Dim trDate As TableRow
Dim tdDate As TableCell
Dim prevDate As Date

Dim cn As New
SqlConnection(ConfigurationSettings.AppSettings("CONN_STRING"))
Dim cm As New SqlCommand("sp_show_games", cn)
cm.CommandType = CommandType.StoredProcedure

cn.Open()

tblPlayGames.CellPadding = 1
tblPlayGames.CellSpacing = 2

Try
Dim dr As SqlDataReader =
cm.ExecuteReader(CommandBehavior.CloseConnection)
Do While dr.Read
'display the date
If prevDate <> dr.GetDateTime(1).ToLongDateString Then
trDate = New TableRow
trDate.BackColor = Color.DarkBlue
tdDate = New TableCell
tdDate.ColumnSpan = 5
tdDate.CssClass = "sm_text"
tdDate.Font.Bold = True
tdDate.ForeColor = Color.White
tdDate.Text = dr.GetDateTime(1).ToLongDateString
trDate.Controls.Add(tdDate)
tblPlayGames.Controls.Add(trDate)
End If

tr = New TableRow
'add an image for the visitor to the cell
vImage = New Image
vImage.ImageUrl = "images/teams/" & dr.GetString(4)
tdVimage = New TableCell
tdVimage.Controls.Add(vImage)
tr.Controls.Add(tdVimage)

'add a radio button for the visitor
td1 = New TableCell
rb1 = New RadioButton
rb1.GroupName = dr.GetInt32(0)
rb1.ID = dr.GetInt32(0) & "_" & dr.GetString(2)
rb1.CssClass = "team_text"
rb1.Text = dr.GetString(2)
td1.Controls.Add(rb1)
tr.Controls.Add(td1)

'add a radio button for the tie
td2 = New TableCell
td2.BackColor = Color.LightGray
rbTie = New RadioButton
rbTie.GroupName = dr.GetInt32(0)
rbTie.ID = dr.GetInt32(0) & "_0"
rbTie.CssClass = "team_text"
rbTie.Text = "Tie"
td2.Controls.Add(rbTie)
tr.Controls.Add(td2)


'add an image for the home team
hImage = New Image
hImage.ImageUrl = "images/teams/" & dr.GetString(5)
tdHimage = New TableCell
tdHimage.Controls.Add(hImage)
tr.Controls.Add(tdHimage)

'add a radio button for the home team
td3 = New TableCell
rb2 = New RadioButton
rb2.GroupName = dr.GetInt32(0)
rb2.ID = dr.GetInt32(0) & "_" & dr.GetString(3)
rb2.CssClass = "team_text"
rb2.Text = dr.GetString(3)
td3.Controls.Add(rb2)
tr.Controls.Add(td3)

'add all controls to the table
tblPlayGames.Controls.Add(tr)
prevDate = dr.GetDateTime(1).ToLongDateString

Loop
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

End Sub
 
R

Rob

Thanks ranganh,

I've tried that and I was getting an invalid cast exception when trying
to cast to a RadioButtonList. I guess I'll have to use a RadioButtonList
to use your example. As it is I'm generating radio buttons on the fly
and assigning them to a group.

I'll test it with A RadioButtonList.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top