1

I want to make a checkBox that is both

Checked = true

and

Enabled = false

How can I do this?

I wrote this code, but it removes Checked from the checkBox

chkDecreaseAbsenceFromExtraWork.Enabled = !SecurityManager.HasAccess(Session, AccessCode.EditDecreaseAbsenceFromExtraWorkIsImpossible); 
mhd.cs
  • 711
  • 2
  • 10
  • 28

2 Answers2

2

If you have a CheckBox Control

<asp:CheckBox ID="CheckBox1" runat="server" />

You set it to checked and disabled

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        CheckBox1.Enabled = false;
        CheckBox1.Checked = true;
    }
}

Eugene Podskal is right in that the values are not submitted. However ViewState will still set the CheckBox to checked after a PostBack.

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "CheckBox is " + CheckBox1.Checked;
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
1

Unfortunately, it seems to be by design feature of the HTML itself - values of disabled inputs will not be submitted?

You can try

  1. Either to use a hidden fields and synchronize its checkbox's value with it in the form - that is if you really need to have a disabled input.
  2. Or you can make that checkbox readonly
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53