DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: <asp:RadioButton /> Determining which is selected without javascript  (Read 8,476 times)

h0meopathic

  • Participant
  • Joined in 2007
  • *
  • Posts: 24
I have 36 radio buttons in a form, all with the same groupName, and all with different IDs.  Is it possible to find out which ID is selected without fishing for it? Surely a dynamic control such as a radiobutton wouldn't have been made without a simple way of finding out which button was selected.

housetier

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,321
    • Gathering Badge
go check out asp:RadioButtonList, it has a bunch of properties that start with "Selected"

h0meopathic

  • Participant
  • Joined in 2007
  • *
  • Posts: 24
You have to make a radiobuttonlist as follows

Code: (html4strict)
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" 
                    RepeatColumns="3" TextAlign="Left">
                    <asp:ListItem Value="1">07:00AM-07:15AM</asp:ListItem> 
                    <asp:ListItem Value="2">07:15AM-07:30AM</asp:ListItem>
                    <asp:ListItem Value="3">07:30AM-07:45AM</asp:ListItem>
                    <asp:ListItem Value="4">07:45AM-08:00AM</asp:ListItem>
                </asp:RadioButtonList>

Then add some statements to make sure its working (inside submitButton_Click). radioButton is my label
Code: (csharp)
if (RadioButtonList1.SelectedIndex > -1)
        {
            radioButton.Text = "<br />you selected: " + RadioButtonList1.SelectedIndex.ToString();
        }
        else
        {
            radioButton.Text = "Select a radio button";
        }

Then attache it to the database like so:
Code: (csharp)
appNameComm = new SqlCommand("INSERT INTO AppointmentName (LastName, FirstName, Email, BirthDate, Slot) " +
                "VALUES (@LastName, @FirstName, @Email, @BirthDate, @Slot)", conn);
.
.
.
appNameComm.Parameters.Add("@Slot", System.Data.SqlDbType.Int, 2);
            appNameComm.Parameters["@Slot"].Value = RadioButtonList1.SelectedIndex.ToString();

housetier

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,321
    • Gathering Badge
be sure to check that SelectedWhatever is not null. Never trust user input and even a selected radio button is user input.

Original address: https://www.donationcoder.com/forum/index.php?topic=19356.0