<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
}
}
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.
Comments