0

I have two checkboxes, what I want to do is, when I check one, the other should be disabled, I always do this in C# Windows application and it is my first try with ASP.NET is there a way to do that without using combocheckboxes? here is my method which does not work:

protected void checkplan0_CheckedChanged(object sender, EventArgs e)
{
    if (checkplan0.Checked == true)
    {
        checkplan1.Enabled = false;
    }
    if (checkplan0.Checked == false)
    {
        checkplan1.Enabled = true;

    }
}
3
  • So are you getting any error ?, or what else ? Commented Jul 5, 2014 at 11:55
  • No error, it is just not doing anything. Commented Jul 5, 2014 at 11:58
  • Check the answers. It might help you. Commented Jul 5, 2014 at 11:59

3 Answers 3

2

Like others have said you'll need to have autopostback="true", also it might be worth considering using radio buttons, as only one radio button in a group can be checked at a time.

<asp:RadioButton id="radioplan0" Checked="True" GroupName="RadioPlan" runat="server" Autopostback="true" />
<asp:RadioButton id="radioplan1" Checked="False" GroupName="RadioPlan" runat="server" Autopostback="true" />

Then nothing needs to be added to the code behind to toggle the other options off.

Sign up to request clarification or add additional context in comments.

Comments

1

Your code seems to be correct, as you mentioned that you are from windows form background I assume this is what you are missing

<asp:CheckBox 
      ID="checkplan0" 
      runat="server" 
      AutoPostBack="true" 
      OnCheckedChanged="checkplan0_CheckedChanged" />

Set AutoPostBack = "true" setting this true will mean on check of checkbox a postback will be sent to server and the code that you have written on Check Change will execute.

2 Comments

Thank you. I have one question, since the checkbox is actually at the bottom of the page, when I click check for example, it always takes me to the top of the screen, is there away I can prevent that and stay where I am at the page? Thank you again.
Yes, you need to set maintainscrollposition read this article msdn.microsoft.com/en-us/library/…
0

Your code seems good. But as you are beginner. You might be missing simple thing in source page.

=> Check whether Your AutoPostBack set to true for your checkbox. If not add AutoPostBack ="true"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.