Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Unchecking and disabling checkboxes in asp.net

Unchecking and disabling checkboxes in asp.net

Scheduled Pinned Locked Moved ASP.NET
csharpasp-nethelptutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    meeram395
    wrote on last edited by
    #1

    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.

    N R N 3 Replies Last reply
    0
    • M meeram395

      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.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M meeram395

        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.

        R Offline
        R Offline
        renjithmp
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M meeram395

          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.

          N Offline
          N Offline
          nitin_vatsus
          wrote on last edited by
          #4

          foreach (control ctrl in page) { if(ctrl is checkbox) { ((checkbox)(ctrl)).enabled = false; ((checkbox)(ctrl)).checked = flase; } }

          M 1 Reply Last reply
          0
          • N nitin_vatsus

            foreach (control ctrl in page) { if(ctrl is checkbox) { ((checkbox)(ctrl)).enabled = false; ((checkbox)(ctrl)).checked = flase; } }

            M Offline
            M Offline
            meeram395
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups