checkbox checked change will disabled the other checkbox












1















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?










share|improve this question

























  • is this webforms?

    – JohnB
    Nov 21 '18 at 6:49











  • yes it is @JohnB

    – GAD
    Nov 21 '18 at 6:50











  • did you tried to use FirstCheckBox CheckedChanged event ? just put secondCheckBox.Enabled= ! firstCheckBox.Checked;

    – MKH
    Nov 21 '18 at 7:06


















1















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?










share|improve this question

























  • is this webforms?

    – JohnB
    Nov 21 '18 at 6:49











  • yes it is @JohnB

    – GAD
    Nov 21 '18 at 6:50











  • did you tried to use FirstCheckBox CheckedChanged event ? just put secondCheckBox.Enabled= ! firstCheckBox.Checked;

    – MKH
    Nov 21 '18 at 7:06
















1












1








1


0






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?










share|improve this question
















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?







c# .net checkbox webforms isenabled






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 8:15









Reza Aghaei

66.1k856163




66.1k856163










asked Nov 21 '18 at 6:47









GADGAD

154




154













  • is this webforms?

    – JohnB
    Nov 21 '18 at 6:49











  • yes it is @JohnB

    – GAD
    Nov 21 '18 at 6:50











  • did you tried to use FirstCheckBox CheckedChanged event ? just put secondCheckBox.Enabled= ! firstCheckBox.Checked;

    – MKH
    Nov 21 '18 at 7:06





















  • is this webforms?

    – JohnB
    Nov 21 '18 at 6:49











  • yes it is @JohnB

    – GAD
    Nov 21 '18 at 6:50











  • did you tried to use FirstCheckBox CheckedChanged event ? just put secondCheckBox.Enabled= ! firstCheckBox.Checked;

    – MKH
    Nov 21 '18 at 7:06



















is this webforms?

– JohnB
Nov 21 '18 at 6:49





is this webforms?

– JohnB
Nov 21 '18 at 6:49













yes it is @JohnB

– GAD
Nov 21 '18 at 6:50





yes it is @JohnB

– GAD
Nov 21 '18 at 6:50













did you tried to use FirstCheckBox CheckedChanged event ? just put secondCheckBox.Enabled= ! firstCheckBox.Checked;

– MKH
Nov 21 '18 at 7:06







did you tried to use FirstCheckBox CheckedChanged event ? just put secondCheckBox.Enabled= ! firstCheckBox.Checked;

– MKH
Nov 21 '18 at 7:06














3 Answers
3






active

oldest

votes


















1














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;





share|improve this answer
























  • For standard setup you should use document.getElementById("<%= firstCheckBox.ClientID %>"). Server controls may rendered with different id value unless ClientIDMode="Static" is used.

    – Tetsuya Yamamoto
    Nov 21 '18 at 7:21



















0














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.






share|improve this answer































    0














    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






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406614%2fcheckbox-checked-change-will-disabled-the-other-checkbox%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      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;





      share|improve this answer
























      • For standard setup you should use document.getElementById("<%= firstCheckBox.ClientID %>"). Server controls may rendered with different id value unless ClientIDMode="Static" is used.

        – Tetsuya Yamamoto
        Nov 21 '18 at 7:21
















      1














      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;





      share|improve this answer
























      • For standard setup you should use document.getElementById("<%= firstCheckBox.ClientID %>"). Server controls may rendered with different id value unless ClientIDMode="Static" is used.

        – Tetsuya Yamamoto
        Nov 21 '18 at 7:21














      1












      1








      1







      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;





      share|improve this answer













      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;






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 21 '18 at 7:06









      Bhargav AbotiBhargav Aboti

      2188




      2188













      • For standard setup you should use document.getElementById("<%= firstCheckBox.ClientID %>"). Server controls may rendered with different id value unless ClientIDMode="Static" is used.

        – Tetsuya Yamamoto
        Nov 21 '18 at 7:21



















      • For standard setup you should use document.getElementById("<%= firstCheckBox.ClientID %>"). Server controls may rendered with different id value unless ClientIDMode="Static" is used.

        – Tetsuya Yamamoto
        Nov 21 '18 at 7:21

















      For standard setup you should use document.getElementById("<%= firstCheckBox.ClientID %>"). Server controls may rendered with different id value unless ClientIDMode="Static" is used.

      – Tetsuya Yamamoto
      Nov 21 '18 at 7:21





      For standard setup you should use document.getElementById("<%= firstCheckBox.ClientID %>"). Server controls may rendered with different id value unless ClientIDMode="Static" is used.

      – Tetsuya Yamamoto
      Nov 21 '18 at 7:21













      0














      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.






      share|improve this answer




























        0














        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.






        share|improve this answer


























          0












          0








          0







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 7:06









          NakulNakul

          1169




          1169























              0














              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






              share|improve this answer




























                0














                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






                share|improve this answer


























                  0












                  0








                  0







                  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






                  share|improve this answer













                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 7:06









                  Tetsuya YamamotoTetsuya Yamamoto

                  15.7k42240




                  15.7k42240






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406614%2fcheckbox-checked-change-will-disabled-the-other-checkbox%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      Npm cannot find a required file even through it is in the searched directory