.net Select only one of two checkboxes? - c#

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;
}
}

<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

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.

Related

checkbox checked change will disabled the other checkbox

I have two checkboxes. If the first checkbox checked, the second checbox will be disabled and if the first checkbox unchecked, the second checkbox will be enabled.
<div class="data">
<asp:CheckBox ID="firstCheckBox" runat="server" CssClass="LabelText" EnableViewState="False" AutoPostBack="True" />
</div>
<div class="data">
<asp:CheckBox ID="secondCheckBox" runat="server" CssClass="LabelText" EnableViewState="False" AutoPostBack="True" />
</div>
Here is my control part at the Page_Load;
if (firstCheckBox.Checked)
{
secondCheckBox.Enabled = false;
}
else
{
secondCheckBox.Enabled = true;
}
When I checked the firstcheckbox, nothing happens to the secondcheckbox. After I checked the second checkbox, secondcheckbox has been checked and disabled.
What am I missing?
You can use javascript to enable or disable checkbox.
Here firstCheckBox & secondCheckBox are the id's of your checkbox.
if(document.getElementById("firstCheckBox").checked = true)
document.getElementById("secondCheckBox").disabled = true;
else
document.getElementById("secondCheckBox").disabled = false;
You can do it with checkBox CheckChanged event.
Please remove condition from load event of your form and add below code.
<div class="data">
<asp:CheckBox ID="firstCheckBox" runat="server" CssClass="LabelText" EnableViewState="False" AutoPostBack="True" OnCheckedChanged="CheckBox1_Check_Clicked" />
</div>
<div class="data">
<asp:CheckBox ID="secondCheckBox" runat="server" CssClass="LabelText" EnableViewState="False" AutoPostBack="True" OnCheckedChanged="CheckBox2_Check_Clicked" />
</div>
protected void CheckBox1_Check_Clicked(Object sender, EventArgs e)
{
if(CheckBox1.Checked==true)
{
CheckBox2.Enable=false;
}
else
{
CheckBox2.Enable=true;
}
}
protected void CheckBox2_Check_Clicked(Object sender, EventArgs e)
{
if(CheckBox2.Checked==true)
{
CheckBox1.Enable=false;
}
else
{
CheckBox1.Enable=true;
}
}
So you should have two CheckChanged events on each checkbox individual. Autopostback true. Same you can do with only one event if you apply some logic.
I assumed that you want to set Enabled state of second checkbox in server-side, hence you should handle CheckedChanged event from first checkbox like this example:
private void firstCheckBox_CheckedChanged(object sender, EventArgs e)
{
secondCheckBox.Enabled = !firstCheckBox.Checked;
}
The problem why the checkbox checked event handler is not triggered is because you're putting the logic inside Page_Load event instead of CheckedChanged event from first checkbox.
Similar issue:
If one checkbox is checked, set the other to unchecked

C#: How do I check the value of a field before Wizard's next step? (NextButtonClick)

When the user clicks to "Continue" after the Wizard's first step I would like check that the value (selected index) of MyRadioBtn is 1. If not, then open a panel and do not proceed to Step 2 of the wizard.
The problem I'm having is the "MyRadioBtn.SelectedIndex" conditional never returns a value of 1.
Front-end:
<asp:RadioButtonList ID="MyRadioBtn" runat="server" AutoPostBack="True">
<asp:ListItem Value="0">No</asp:ListItem>
<asp:ListItem Value="1">Yes</asp:ListItem>
</asp:RadioButtonList>
Code-behind:
protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
if (Wizard1.ActiveStepIndex == 0)
{
if (MyRadioBtn.SelectedIndex == 1)
{
//Stop the user from moving on to step 2
e.Cancel = true;
//Show the user a panel
WarningPanel.Visible = true;
}
else {
//Continue to STEP 2
}
}
}
Do you need to fire a postbkack after the user selects an item from de DropDownList? If not, change AutoPostBack="True" to AutoPostBack="False"

How to force enable/disable of controls in aspx?

I have a simple windows form app which I have to migrate to run on a webpage, so I'm trying with aspx(c#).
I have two radio buttons, but at time only one of them should be checked. I implemented the very same code from the original app, but it's not working:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked == true)
{
RadioButton2.Checked = false;
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton2.Checked == true)
{
RadioButton1.Checked = false;
}
}
So why these changes not being applied on the page?
you can use GroupName Property of RadioButton Control.
if you set the same GroupName for set of RadioButtons you don't need to write the Code Behid which you have written in the above Post/Question, as only one radio button can be selected from the group.
but if you want to invoke some action like Disabling TextBox on Particular Radio Button click event you can use following sample code.
Example: in this example i'm taking 3 Radio Buttons all set to Same GroupName, hence only one RadioButton canbe selected at a time.
when user selects/checks a RadioButton1 i'm Disabling the TextBox1.
Note : Please Make sure that your RadioButton AutoPostBack Property set to True otherwise events on RadioButton won't be fired.
Design Code:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RadioButton ID="RadioButton1" runat="server" Text="DisableTextBox" GroupName="Group1" AutoPostBack="True" OnCheckedChanged="RadioButton1_CheckedChanged"/>
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="Group1" AutoPostBack="True"/>
<asp:RadioButton ID="RadioButton3" runat="server" GroupName="Group1" AutoPostBack="True" />
Code Behind:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked)
TextBox3.Enabled = false;
else
TextBox3.Enabled = true;
}
Put the checkboxes in a GroubBox or Panel
You can set the GroupName of both radiobuttons the same, so the Code Behind you wrote is not needed anymore.
Also, check out the RadioButtonList
UPDATE
Your aspx-code should look something like this:
<asp:RadioButton id="RadioButtonEnabled" runat="server" Text="Enabled" GroupName="GroupTxtEnabled" AutoPostBack="True" OnCheckedChanged="RadioButtonEnabled_CheckedChanged"/>
<asp:RadioButton id="RadioButtonDisabled" runat="server" Text="Disabled" GroupName="GroupTxtEnabled" AutoPostBack="True"/>
<asp:TextBox id="TextBox1" runat="server"/>
And in your code-behind:
protected void RadioButtonEnabled_CheckedChanged (object sender, EventArgs e)
{
TextBox1 = RadioButtonEnabled.Checked
}
As you can see, I've used the OnCheckedChanged only at one radiobutton, since both radiobuttons are changed at the same time (property GroupName is equal).

How to deselect radiobutton when dropdown value is selected

I have 2 radio buttons and a drop down list under option 1 and option 2. the user can either select the radio button or the drop down list.
option 1
radiobtn1
radiobtn2
option2
dropdownlistbox
the radiobtn1 under option1 is checked as default. When the user select the value from the dropdown list, both the radio buttons should be disabled or it should deselects the radiobtn1 which is checked as default.
<tr><td>option1</td></tr>
<tr>
<td><asp:RadioButton ID="rdo" GroupName="Month" Text="radiobtn1" runat="server /></td>
<td><asp:RadioButton ID="rdo2" GroupName="Month" Text="radiobtn2" runat="server /></td>
</tr>
<tr><td>option2</td></tr>
<tr><td>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="ddMonYear_SelectedIndexChanged">
<asp:ListItem Text="opt1"></asp:ListItem>
<asp:ListItem Text="opt2"></asp:ListItem>
</asp:DropDownList>
</td></tr>
protected void Page_Load(object sender, EventArgs e)
{
rdo.Checked=true;
if (ddMonYear.SelectedValue.Length.ToString() != "0")
{
rdo.Checked = false;
rdo.Enabled = false;
}
else
{
rdo.Checked = true;
}
}
The above code doesnt work. the radiobtn 1 is not deselecting when i select the value from the drop down.
Help me in correcting my code.
Thanks.
On your dropdown box, add this code (autopostback)
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="ddMonYear_SelectedIndexChanged" AutoPostBack=True">
What this does is keep the value of the dropdown box. Each time your page is loaded, the dropdown box gets the default value before your pageload event fires. give that a try.

Identifying object in ItemTemplate and event not firing

I am using a RadListView control with an ItemTemplate that contains a button as shown here:
<ItemTemplate>
<tr class="rlvI">
//more TD elements here
<td>
<telerik:RadButton ID="ENABLEDToggle" runat="server" Width="75" ButtonType="StandardButton" AutoPostBack="true"
ToggleType="CustomToggle" Checked='<%# Enabled_Converter(Eval("ENABLED")) %>' OnCheckedChanged="TaskStateChange_Clicked">
<ToggleStates>
<telerik:RadButtonToggleState Text="Enabled" />
<telerik:RadButtonToggleState Text="Disabled" />
</ToggleStates>
</telerik:RadButton>
</td>
</tr>
</ItemTemplate>
My first question is why is it that when my button is pressed, I do not enter my TaskStateChange_Clicked event handler? It is as if the event is never fired.
Second, whenever a button is clicked, how do I get the object associated with that row?
As for the first question, check how you are binding your RadListView. Problems like this usually appear if controls are bound with data on every postback. So if you have something like
void Page_Load(object sender, EventArgs e)
{
...
RadListView1.DataSource = dataSource;
RadListView1.DataBind();
...
}
replace it with
void Page_Load(object sender, EventArgs e)
{
...
if (!this.IsPostBack)
{
RadListView1.DataSource = dataSource;
RadListView1.DataBind();
}
...
}
Update from comments. Another reason might be that the type of your button is StandardButton while spec implies that event CheckedChanged is fired only when button type is ToggleButton.
As for the second question consider using RadListView's ItemCommand event. This way you can make use of CommandArgument property of RadButton and pass any info you want in it, say object's ID.

Categories

Resources