Unchecking and disabling checkboxes in asp.net
-
I have four individual checkboxes in my form called as General, Direct, Indirect and Private. The issue is that when private is checked all others should be disabled and unchecked if checked. I have the following code for that: if (PrivateCheckBox.Checked) { GeneralCheckBox.Checked = false; GeneralCheckBox.Enabled = false; IndirectCheckBox.Checked = false; IndirectCheckBox.Enabled = false; DirectCheckBox.Checked = false; DirectCheckBox.Enabled = false; } else { GeneralCheckBox.Checked = true; GeneralCheckBox.Enabled = true; IndirectCheckBox.Checked = true; IndirectCheckBox.Enabled = true; DirectCheckBox.Checked = true; DirectCheckBox.Enabled = true; } This serves the purpose but simply increases coding lines which I don't want. I can use the ternary operator, but only one operation can do with that, either checked or enabled. Can anybody have any idea how to achieve this with minimum line of code? I am not able to think of any other option.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
-
I have four individual checkboxes in my form called as General, Direct, Indirect and Private. The issue is that when private is checked all others should be disabled and unchecked if checked. I have the following code for that: if (PrivateCheckBox.Checked) { GeneralCheckBox.Checked = false; GeneralCheckBox.Enabled = false; IndirectCheckBox.Checked = false; IndirectCheckBox.Enabled = false; DirectCheckBox.Checked = false; DirectCheckBox.Enabled = false; } else { GeneralCheckBox.Checked = true; GeneralCheckBox.Enabled = true; IndirectCheckBox.Checked = true; IndirectCheckBox.Enabled = true; DirectCheckBox.Checked = true; DirectCheckBox.Enabled = true; } This serves the purpose but simply increases coding lines which I don't want. I can use the ternary operator, but only one operation can do with that, either checked or enabled. Can anybody have any idea how to achieve this with minimum line of code? I am not able to think of any other option.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
GeneralCheckBox.Checked = GeneralCheckBox.Enabled = !PrivateCheckBox.Checked; IndirectCheckBox.Checked = IndirectCheckBox.Enabled = !PrivateCheckBox.Checked; DirectCheckBox.Checked = DirectCheckBox.Enabled = !PrivateCheckBox.Checked;
only two letters away from being an asset
-
I have four individual checkboxes in my form called as General, Direct, Indirect and Private. The issue is that when private is checked all others should be disabled and unchecked if checked. I have the following code for that: if (PrivateCheckBox.Checked) { GeneralCheckBox.Checked = false; GeneralCheckBox.Enabled = false; IndirectCheckBox.Checked = false; IndirectCheckBox.Enabled = false; DirectCheckBox.Checked = false; DirectCheckBox.Enabled = false; } else { GeneralCheckBox.Checked = true; GeneralCheckBox.Enabled = true; IndirectCheckBox.Checked = true; IndirectCheckBox.Enabled = true; DirectCheckBox.Checked = true; DirectCheckBox.Enabled = true; } This serves the purpose but simply increases coding lines which I don't want. I can use the ternary operator, but only one operation can do with that, either checked or enabled. Can anybody have any idea how to achieve this with minimum line of code? I am not able to think of any other option.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
Add the check boxes (except private check box ) to a checkbox group. and use this code foreach (CheckBox ch in CheckBoxList1.Items) { if(private.checked) { ch.Checked = false; ch.Enabled = false; } else { ch.Checked = true; ch.Enabled = true; } } If you want to do similar kind of operation to a group of items then this is the standard way of doing that. Hope this help
-
I have four individual checkboxes in my form called as General, Direct, Indirect and Private. The issue is that when private is checked all others should be disabled and unchecked if checked. I have the following code for that: if (PrivateCheckBox.Checked) { GeneralCheckBox.Checked = false; GeneralCheckBox.Enabled = false; IndirectCheckBox.Checked = false; IndirectCheckBox.Enabled = false; DirectCheckBox.Checked = false; DirectCheckBox.Enabled = false; } else { GeneralCheckBox.Checked = true; GeneralCheckBox.Enabled = true; IndirectCheckBox.Checked = true; IndirectCheckBox.Enabled = true; DirectCheckBox.Checked = true; DirectCheckBox.Enabled = true; } This serves the purpose but simply increases coding lines which I don't want. I can use the ternary operator, but only one operation can do with that, either checked or enabled. Can anybody have any idea how to achieve this with minimum line of code? I am not able to think of any other option.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
foreach (control ctrl in page) { if(ctrl is checkbox) { ((checkbox)(ctrl)).enabled = false; ((checkbox)(ctrl)).checked = flase; } }
-
foreach (control ctrl in page) { if(ctrl is checkbox) { ((checkbox)(ctrl)).enabled = false; ((checkbox)(ctrl)).checked = flase; } }
This will not work, as it is giving the error as " foreach cannot operate on variables of type System.Web.UI.Page because System.Web.UI.Page does not contain a public definition for 'GetEnumerator'"
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.