1

I have an asp:checkbox with OnClick="return false" which 'closes' it for changes (that's the nearest equivalent to ReadOnly=true which I found).
I also have a button.
When I click the button I need the codebehind (C#) to change the checkbox to "return true", that is : to 'open' it for checking or unchecking. Clicking the button again should 'close' it back again.

In short - how do I toggle between these two states in the code behind?

HTML:

<asp:Button runat="server" ID="my_Button" Text="click me" OnClick="my_Button_Click" />
<asp:CheckBox runat="server" ID="my_CheckBox" Checked="true" OnClick="return false;" />

Code behind:

    protected void my_Button_Click(object sender, EventArgs e)
    {
       // here is where the toggling should come...     <============
    }
gadi
  • 481
  • 3
  • 14
  • 32
  • 1
    Sounds like you just need to set the Checkbox's Enabled property to False inside your click event. I think you'll want to use an ASP UpdatePanel to wrap the checkbox controls, otherwise the whole page will probably get reloaded. – 0_______0 Dec 13 '15 at 22:46

2 Answers2

2

As you can see here: OnClick vs OnClientClick for an asp:CheckBox? - there's no OnClick for checkbox. You should use OnCheckedChanged instead. I've attached a minor fix to the problem:

<asp:Button runat="server" ID="my_Button" Text="click me" OnClick="my_Button_Click" />
    <asp:CheckBox runat="server" ID="my_CheckBox" Checked="true" OnCheckedChanged="my_checkbox_click" />

And the code behind:

protected void my_checkbox_click(object sender, EventArgs e)
    {
    }

    protected void my_Button_Click(object sender, EventArgs e)
    {
        my_CheckBox.Enabled = !my_CheckBox.Enabled;            
    }
Community
  • 1
  • 1
  • Thanks but I think I wasn't clear enough... I don't want any event to fire when clicking the checkbox. What I need is for the checkbox to change from `OnClick="return false"` to `OnClick="return true"` (and back) whenever the **button** is clicked. Anyhow, I think I found how to do it and will post the answer here after I verify it works. – gadi Dec 14 '15 at 14:51
0

In order to achieve this, I added a HiddenField and used it in the code behind.
HTML:

<asp:HiddenField runat="server" ID="my_Hidden_Field" Value="readonly" />
<asp:Button runat="server" ID="my_Button" Text="click me" OnClick="my_Button_Click" />
<asp:CheckBox runat="server" ID="my_CheckBox" Checked="true" OnClick="return false;" />

Code behind:

    protected void my_Button_Click(object sender, EventArgs e)
    {
        if (my_Hidden_Field.Value == "readonly")
        {
            my_Hidden_Field.Value = "canwrite";
            my_CheckBox.Attributes.Add("onclick", "return true;");
        }
        else
        {
            my_Hidden_Field.Value = "readonly";
            my_CheckBox.Attributes.Add("onclick", "return false;");
        }
    }

(like always, I look for a solution for a day or two, give up, post a question here, and then I come up with an answer... Hope this will be helpful to others :-)

gadi
  • 481
  • 3
  • 14
  • 32