-3

I have 3 check boxes and I would like to write a statement in C# that will allow me to find out which of the boxes is checked. I tried using

 if(check1.IsChecked.Value == 1){
}

But that didnt seem to work. How can I solve this? I would also like to be able to get the text belonging to the checked check box. Here is the HTML code for the check boxes.

<div>
       <h3>Membership Type</h3>
  <label>
    <input type="checkbox" class="radio" value="1" name="check1" />Normal User</label>
  <label>
    <input type="checkbox" class="radio" value="1" name="check2" />Verified Reviewer</label>
  <label>
    <input type="checkbox" class="radio" value="1" name="check3" />Development Team</label>

        </div>
ninesalt
  • 4,054
  • 5
  • 35
  • 75

3 Answers3

1

I can't tell if you are expecting a radio button style, where only one option is available at a time, or checkbox style which would allow any combination of selections. This may be what you are after:

<asp:RadioButtonList ID="rblUserType" runat="server">
    <asp:ListItem Text="Normal User" Value="1" />
    <asp:ListItem Text="Verified Reviewer" Value="2" />
    <asp:ListItem Text="Development Team" Value="3" />
</asp:RadioButtonList>

Code side:

if (rblUserType.SelectedIndex == 1)
{
    //First value selected
}
else if (rblUserType.SelectedValue == "2")
{
    //An alternate way to select which entry was selected
}
Aki
  • 107
  • 8
0

Only the ID of a .Net control is mapped to a variable, the name is not. Assign a value to the ID tag parameter and use runat="server" to be able to access it.

cdonner
  • 37,019
  • 22
  • 105
  • 153
0

Looks like you have check boxes as HTML control. You cant access those in c# code behind. Instead you can use javascript code to get text of selected checkbox or mark the control as runat=Server

CODE
  • 1
  • 1