9

I have a input checkbox that is checked and disabled when some JS runs on page load.

<input name="selectUnionDues" id="selectUnionDues"  type="checkbox" checked="checked" runat="server" />

When I get the value of the checkbox by using (on the server side)

this.selectUnionDues.checked //returns false

I always get a false

EDIT: I am concerned about using heavy asp.net controls since the page size in this application needs to be low. Is there a way out using HTMl controls?

DotnetDude
  • 11,617
  • 35
  • 100
  • 158
  • Here are the alternatives; https://stackoverflow.com/questions/4727974/how-to-post-submit-an-input-checkbox-that-is-disabled/60424933#60424933 – Mahib Feb 27 '20 at 01:12

6 Answers6

11

Disabled form controls are not "successful controls", that means they values aren't "submitted" at all with your form.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
3

check this out https://web.archive.org/web/20211020150718/https://www.4guysfromrolla.com/articles/012506-1.aspx

In short, (1)there is a "SubmitDisabledControls" property of "form" or (2)tweak the javascript the author provided

Might
  • 305
  • 3
  • 12
  • This doesn't always work; I was disabling tables-full of controls by the method suggested [here](http://forums.asp.net/p/1121242/3109517.aspx) and it prevented `SubmitDisabledControls` from working properly. I had to disable all the controls in the table separately to get it to behave. But thanks for the tip. – Brian Hooper Nov 24 '11 at 15:23
1

If you disable the checkbox, it always return false. the best idea is to make the checkbox readonly, so that it will return the value as it is. make the readonly="readonly".

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • This MDN article specifically says to _not_ make checkboxes readonly: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly – rpearce Sep 18 '20 at 14:04
0

Although the original question was about HtmlInputCheckBox control, I think it will be useful to mention that similar problem also exists for Checkbox control, for which the picture is more complicated, because there is a difference between Enabled="False" and disabled="disabled". I post results of the investigation here: ASP.Net Checkbox.Checked is set to default when use disabled attribute or difference between Enabled property and disabled attribute. Specifically it explains why Checked property is reset when disabled attribute is used, and why it is not reset when Enabled server property is used.

alex
  • 187
  • 1
  • 9
0

I get less than intuitive behavior as well. If I test the value in Page_Init, it always returns true. Testing it in Page_Load returns the selected value.

Try using a server side control instead of an HTML control with runat=server.

<asp:CheckBox ID="selectUnionDues" runat="server" Checked="true" />

Also, you state "disabled" control but I don't see anything that is disabled in your markup. Either way, better to use a server WebControl.

andleer
  • 22,388
  • 8
  • 62
  • 82
  • Between Page_Init and Page_Load there are more page lifecycle methods (which you can override if you want to): LoadViewState and LoadPostData. Both of these might change the values in the controls. – Andrew Shepherd Mar 09 '09 at 23:22
0

If you don't mind relying on JS, you could have a hidden input who's value changes when the checkbox's check property changes and then use that value server side.

Charlino
  • 15,802
  • 3
  • 58
  • 74
  • Btw, I'm guess it wouldn't be a problem relying on JS because JS is what is causing the checkbox to become disabled. – Charlino Mar 09 '09 at 23:12