mhd.cs
mhd.cs

Reputation: 721

How to Set Checked and Enabled Property On CheckBox?

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); 

Upvotes: 1

Views: 808

Answers (2)

VDWWD
VDWWD

Reputation: 35514

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;
}

Upvotes: 2

Eugene Podskal
Eugene Podskal

Reputation: 10401

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

Upvotes: 1

Related Questions