Srb1313711
Srb1313711

Reputation: 2047

.net Select only one of two checkboxes?

I have two checkboxes a true and a false. The user should only be allowed to select one, when one is selected the other is deselected and vice versa. I have an on change event which does this, but If i select one so the the other deselects and then press the browser back button they both appear selected?! I put in a method to check for this event, but when they both appeared select on the front end the logic in the back end was seeing one as being unselected? Does this make sense?

My Check boxes:

<asp:PlaceHolder runat="server" ID="phIsValidated">
            <asp:CheckBox ID="chbTrue"
                Text="True"
                runat="server"
                AutoPostBack="True"
                OnCheckedChanged="Check_Clicked" />
            <asp:CheckBox ID="chbFalse"
                Text="False"
                runat="server"
                AutoPostBack="True"
                OnCheckedChanged="Check_Clicked" />
        </asp:PlaceHolder>

My on changed event:

   protected void Check_Clicked(Object sender, EventArgs e)
    {
        CheckBox checkBox = (CheckBox)sender;
        if (checkBox.ID=="chbTrue")
        {
            chbFalse.Checked = !chbTrue.Checked;
        }
        else
        {
            chbTrue.Checked = !chbFalse.Checked;
        }
    }

Upvotes: 0

Views: 2437

Answers (2)

Shivam Shil
Shivam Shil

Reputation: 11

Step 1: Add two Checkboxes.

Step 2: Add OnCheckedChanged Event to all the checkboxes.

(In case if you do not know How to Add OnChecKedChanged Event, You can follow these steps.

There are two ways to Add this.

1st You can directly add It to your code like

<asp:CheckBox  runat="server" ID="Try1ID" OnCheckedChanged="Try1ID_CheckedChanged"/>

Or

2nd You can open the design tab in visual studio and double click on the checkbox you want to create OnChecKedChanged event.

)

Step 3: Set AutoPostBack = True.

( Example :

<asp:CheckBox  runat="server" ID="Try1ID" AutoPostBack="True" OnCheckedChanged="Try1ID_CheckedChanged"/>

or

you can set this from checkbox property window.

)

Step 4 : Add If Else Condition in your .cs file inside the OnChecKedChanged Event

(

Example: If You have two checkboxes with names Try1ID_CheckedChanged and Try2ID_CheckedChanged Your condition should look like this,

protected void Try1ID_CheckedChanged(object sender, EventArgs e)
                {
                    if (Try1ID.Checked)
                        {
                                Try2ID.Checked = false;
                        }
                }
    
    protected void Try2ID_CheckedChanged(object sender, EventArgs e)
                {
                    if (Try2ID.Checked)
                        {
                                Try1ID.Checked = false;
                        }
                }

)

Step 5: You Press F5 and test the program.

Upvotes: 0

Lon Prosser
Lon Prosser

Reputation: 232

<asp:RadioButtonList ID="MyRadioButtons" runat="server">
      <asp:ListItem Value="Y">Yes</asp:ListItem>
      <asp:ListItem Value="N">No</asp:ListItem>
</asp:RadioButtonList>

Here is an example with code to process the selected item: http://www.w3schools.com/aspnet/showaspx.asp?filename=demo_radiobuttonlist

Upvotes: 1

Related Questions