How to retrieve which radio button within a LI is selected

SearchForKnowledge
<ul class="ulGroup" runat="server" id="ulGroup">
    <li>
        <input type="radio" name="grouptype" id="rdGroup1" class="css-checkbox" value="Group 1" checked="checked" runat="server" />
        <label for="rdGroup1" class="css-label radGroup1">Group 1</label>
    </li>
    <li>
        <input type="radio" name="grouptype" id="rdGroup2" class="css-checkbox" value="Group 2" runat="server" />
        <label for="rdGroup2" class="css-label radGroup1">Group 2</label>
    </li>
    <li>
        <input type="radio" name="grouptype" id="rdGroup3" class="css-checkbox" value="Group 3" runat="server" />
        <label for="rdGroup3" class="css-label radGroup1">Group 3</label>
    </li>
    <li>
        <input type="radio" name="grouptype" id="rdGroup4" class="css-checkbox" value="Group 4" runat="server" />
        <label for="rdGroup4" class="css-label radGroup1">Group 4</label>
    </li>
</ul>

How do I retrieve which li radio button is selected from code-behind.

When I run the code, I get the following error:

Parser Error Message: Cannot create an object of type 'System.Boolean' from its string representation 'checked' for the 'Checked' property.

using (SqlConnection sc = new SqlConnection(gloString))
{
    using (SqlCommand qSave = new SqlCommand())
    {
        qSave.Connection = sc;
        qSave.CommandType = CommandType.Text;
        qSave.CommandText = @"INSERT INTO [db1].[dbo].[tbl1] (FirstName, LastName, EmailAddress, UN, UP, DietGroup) VALUES (@FirstName, @LastName, @EmailAddress, @UN, @UP, @DietGroup)";
        qSave.Parameters.AddWithValue("@FirstName", txtFirst.Value);
        qSave.Parameters.AddWithValue("@LastName", txtLast.Value);
        qSave.Parameters.AddWithValue("@EmailAddress", txtEmail.Value);
        qSave.Parameters.AddWithValue("@UN", txtUser.Value);
        qSave.Parameters.AddWithValue("@UP", txtPass.Value);
        qSave.Parameters.AddWithValue("@DietGroup", rdGroup1.Checked); //Instead of rdGroup1.Checked, I would like to get which of the four radio is checked
    }
}
Enrique Zavaleta

You should use this markup instead of yours

<asp:RadioButtonList ID="ulGroup" runat="server">
    <asp:ListItem Text="Group 1" Value="Group 1" Selected="True"/>
    <asp:ListItem Text="Group 2" Value="Group 2" />
    <asp:ListItem Text="Group 3" Value="Group 3" />
    <asp:ListItem Text="Group 4" Value="Group 4" />
</asp:RadioButtonList>

and in your code behind change this

qSave.Parameters.AddWithValue("@DietGroup", rdGroup1.Checked); 

to this

qSave.Parameters.AddWithValue("@DietGroup", ulGroup.SelectedValue); 

EDIT

If you want to maintain your markup, then just change your checked="checked" to checked="true", also add the next using

using System.Web.UI.HtmlControls;

then create a function like this

public string getSelectedRadioButton()
    {
        string radioSelected = string.Empty;
        foreach (Control control in ulGroup.Controls)
        {
            if (control.GetType().Name.Equals("HtmlInputRadioButton") && (control as HtmlInputRadioButton).Checked)
            {
                radioSelected = (control as HtmlInputRadioButton).Value; break;
            }
        }
        return radioSelected;
    }

now you can get the selected in this way

qSave.Parameters.AddWithValue("@DietGroup", getSelectedRadioButton());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to get value of selected radio button?

From Java

How can I know which radio button is selected via jQuery?

From Dev

How to check if the radio button is selected or not in Selenium WebDriver?

From Dev

How to know which radio button is selected in jquery?

From Dev

How to check if radio buttons are selected on button click?

From Dev

how to know which radio button id selected of which row?

From Dev

How To get Selected Radio Button Value in Mvc

From Dev

How to get the value of the selected radio button?

From Dev

How to get selected radio button from ToggleGroup

From Dev

How to get value of selected radio button in angularjs

From Dev

How to display a radio button selected using a variable?

From Dev

How can I tell which radio button is selected with node.js

From Dev

How to find out which radio button the user selected in Javascript?

From Dev

How to get selected radio button values?

From Dev

How to check which radio button is selected if they're all runat server?

From Dev

How to get the pk of the selected radio button in django

From Dev

How to check if the radio button is selected or not in Selenium WebDriver?

From Dev

Servlet retrieve pre selected radio button

From Dev

How to get last selected radio button id

From Dev

How to get list of values which are selected in radio button after click on submit in Angular js

From Dev

how to know which radio button is selected out of all options?

From Dev

How to check which radio button is selected if they're all runat server?

From Dev

How to make radio button selected by default?

From Dev

How to style selected radio button

From Dev

How to swap the displayed image based on which radio button is selected?

From Dev

How to disable buttons based on which radio button is selected?

From Dev

How get value of selected radio button in datalist

From Dev

How to retrieve selected value of radio button from fetch array result..?

From Dev

How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)

Related Related

  1. 1

    How to get value of selected radio button?

  2. 2

    How can I know which radio button is selected via jQuery?

  3. 3

    How to check if the radio button is selected or not in Selenium WebDriver?

  4. 4

    How to know which radio button is selected in jquery?

  5. 5

    How to check if radio buttons are selected on button click?

  6. 6

    how to know which radio button id selected of which row?

  7. 7

    How To get Selected Radio Button Value in Mvc

  8. 8

    How to get the value of the selected radio button?

  9. 9

    How to get selected radio button from ToggleGroup

  10. 10

    How to get value of selected radio button in angularjs

  11. 11

    How to display a radio button selected using a variable?

  12. 12

    How can I tell which radio button is selected with node.js

  13. 13

    How to find out which radio button the user selected in Javascript?

  14. 14

    How to get selected radio button values?

  15. 15

    How to check which radio button is selected if they're all runat server?

  16. 16

    How to get the pk of the selected radio button in django

  17. 17

    How to check if the radio button is selected or not in Selenium WebDriver?

  18. 18

    Servlet retrieve pre selected radio button

  19. 19

    How to get last selected radio button id

  20. 20

    How to get list of values which are selected in radio button after click on submit in Angular js

  21. 21

    how to know which radio button is selected out of all options?

  22. 22

    How to check which radio button is selected if they're all runat server?

  23. 23

    How to make radio button selected by default?

  24. 24

    How to style selected radio button

  25. 25

    How to swap the displayed image based on which radio button is selected?

  26. 26

    How to disable buttons based on which radio button is selected?

  27. 27

    How get value of selected radio button in datalist

  28. 28

    How to retrieve selected value of radio button from fetch array result..?

  29. 29

    How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)

HotTag

Archive