views:

502

answers:

5

I have a checkbox which in some cases may be disabled and checked using javascript, i.e:

var cbTest = document.getElementById("CheckBoxTest");
cbTest.disabled = true;
cbTest.checked = true;

However, when sending a postback, CheckBoxTest.Checked is false on the server side.

Is it possible to disable the checkbox and still have the server side recognize it as checked?

A: 

To get a disabled field's value you'll have to enable it just before the postback. The avalue of a disabled field is never send.

remi bourgarel
+2  A: 

Any disabled control inside a form will not submitted to server on post back. You can add a hidden input control and put the checkbox state to that input and server side check that hidden input value.

Emrah GOZCU
I thought of using a hidden field, but was hoping there is a better way. Apparently it is the best way to go after all. Thanks.
Kuzco
A: 

I think that HTML forms only post the values of enabled controls. I guess one way around this would be to have a hidden field (<input type="hidden"/>), and to store the Checked status of the CheckBox in here.

Graham Clark
A: 

You can either enable the checkbox before the post, or set up a hidden field to hold the value of the checked state. A disabled control won't post its value.

kekekela
A: 

Have you tried to get the Attribute like

string val = checkbox.Attributes["checked"].ToString();
EduardoMello
I checked it now, and the attribute is null.
Kuzco