checkbox checked change will disabled the other checkbox - c#

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

Related

How do I make a postback trigger concurrently?

I have a TextboxSectionName, TextboxSectioncode and Checkbox. When Checkbox gets checked, a text in the TextboxSectionname will be added into TextboxSectioncode, TextboxSectioncode will be read-only, and gets background color; When unchecked, TextboxsectionName gets back.
The problem is process above works fine, but if I try to type more text into textboxSectionName when TextboxSectionCode is still checked, TextboxSectionCode doesn't take a text that has been added to textboxSectionName. To get it working, I have to uncheck the checkbox and check it again.
Is there any way to make the TextboxSectionCode take newly-added text from TextboxSectionName?
Here I have this block of code.
aspx
<asp:CheckBox ID="CheckBoxSectionCode" runat="server" Style="margin-left: 5px;" OnCheckedChanged="CheckBoxSectionCode_CheckedChanged" AutoPostBack="True" />
<asp:TextBox ID="TextBoxSectionName" runat="server" Width="250px" onkeyup="RefreshUpdatePanel();" ></asp:TextBox>
<asp:TextBox ID="TextBoxSectionCode" runat="server" Width="250px" ></asp:TextBox>
code behind checkbox event handler
protected void CheckBoxSectionCode_CheckedChanged(object sender, EventArgs e)
{
if (CheckBoxSectionCode.Checked == true)
{
TextBoxSectionCode.Text = TextBoxSectionName.Text;
TextBoxSectionCode.BackColor = Color.LightGray;
TextBoxSectionCode.ReadOnly = true;
}
else
{
TextBoxSectionCode.ReadOnly = false;
TextBoxSectionCode.Text = "";
TextBoxSectionCode.BackColor = Color.White;
}
}
Thanks,
Phillip

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

AjaxControlToolkit ComboBox cannot be enabled after disabling

I have an AjaxControlToolkit ComboBox as follows:
<ATC:ComboBox
id="cmbid1"
runat="server"
Width="150px"
RenderMode="Block"
DropDownStyle="DropDownList"
AutoCompleteMode="SuggestAppend">
</ATC:ComboBox>
Elsewhere on the page, I have a checkbox that I want to use to toggle the ComboBox:
<asp:CheckBox
ID="lockCheckBox"
runat="server"
Text="Locked"
Enabled="False"
OnCheckedChanged="lockCheckBox_CheckedChanged"
AutoPostBack="True" />
Then I have a handler like this:
protected void lockCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (lockCheckBox.Checked)
{
cmbid1.Enabled = false;
EnableControlNotLockedDown();
}
else
{
cmbid1.Enabled = true;
}
}
Instead of toggling the ComboBox from enabled to disabled and vice versa, the ComoBox just gets disabled and never becomes enabled again.
What am I doing wrong?

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).

RadComboBox pre selection

First of all what I want to do is to pre select a value in my RadComboBox ,and if this value is not selected something else is selected then change the visibility to of some specific fields hidden.
My problem is that I'm able to make my pre select but somehow I can not change the status of my visibility for my specific fields when this pre selected value has changed.
What I have tired is to do it with a standard event OnSelectedIndexChanged but some how this is not triggering why so ever.. I have also added AutoPostBack=true as well as ViewStateMode=Enabled"
First my field's
Here comes my preslect as well here I would like to trigger the visibility change
<div class="formRowDiv">
<asp:Label ID="Activitylbl" runat="server" Text="Activity" CssClass="formLabel" />
<telerik:RadComboBox ID="rcbActivity" CssClass="rowForm" ViewStateMode="Enabled" runat="server" Width="260px" EmptyMessage="- Activity -"
DataTextField="ActivityId" DataValueField="ActivityId" AutoPostBack="true" OnSelectedIndexChanged="rcbActivity_SelectedIndexChanged">
</telerik:RadComboBox>
<asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="rcbActivity"
ErrorMessage="Can not be empty" CssClass="rowFormValidation" />
</div>
What I want to hide:
<div class="formRowDiv">
<asp:Label ID="ActivityDescription" runat="server" Text="ActivityDescription" CssClass="formLabel" Visible="false"/>
<telerik:RadTextBox runat="server" ID="rtbActivityDescription" Wrap="true" Height="50" TextMode="MultiLine" AutoPostBack="true" CssClass="rowForm" ReadOnly="true" Visible="false" />
</div>
How I do my pre selection :
In my databind method that is called in my Page_Load
I firrst loop and then do a pre select
foreach (Activity item in ctx.Activity.OrderBy(l =>l.Code))
{
rcbActivity.Items.Add(new RadComboBoxItem(item.FullActivity, item.ActivityId.ToString()));
if (rcbActivity.Items.FindItemByValue("4") != null)
{
rcbActivity.SelectedIndex = rcbActivity.Items.IndexOf(rcbActivity.Items.FindItemByValue("4"));
ActivityDescription.Visible = true;
rtbActivityDescription.Visible = true;
rtbActivityDescription.ReadOnly = false;
}
}
Here is how I would hide my Fields
protected void rcbActivity_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
ActivityDescription.Visible = true;
rtbActivityDescription.Visible = true;
rtbActivityDescription.ReadOnly = false;
}
In case your controls are in an update panel then try removing it if the update panel is not so important and see if the changes u make to the controls in the server side are getting affected properly

Categories

Resources