26

We have a checkbox that is initially disabled and checked. It is then enabled on the client side through javascript. If the user then unchecks the box and presses the button to invoke a postback, the state of the checkbox remains as checked on the server side. This is obviously undesirable behaviour. Here is an example.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testcb.aspx.cs" Inherits="ESC.testcb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <script type="text/javascript">
        function buttonClick() {
            var cb = document.getElementById('<%= CheckBox1.ClientID %>');
            cb.disabled = false;
            cb.parentNode.disabled = false;
        }


    </script>

    <div>
        <asp:CheckBox ID="CheckBox1" runat="server" Checked="true" Enabled="false" />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="buttonClick(); return false;" />
        <asp:Button ID="Button2" runat="server" Text="Button2" OnClick="button2Click" />
    </div>
    </form>
</body>
</html>

And the Server-side code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ESC
{
    public partial class testcb : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void button2Click(object sender, EventArgs e)
        {
            string h = "";
        }
    }
}

So we break at the "string h" line and check the value of CheckBox1.Checked. It is true, even if it is unchecked on the form.

Anthony
  • 12,177
  • 9
  • 69
  • 105

6 Answers6

36

This is a known problem with ASP.NET - for some reason ASP.NET won't update a checkbox on postback if it was disabled during page load and not checked for postback. I don't know exactly why that is though - if you make the checkbox unselected by default and select it, the value is changed on the server correctly.

The workaround is to add a hidden field to the page that represents the state of the checkbox, then update the field's value to "ON" or "OFF" for example, whenever the checkbox is clicked.

Then on the server you check the value of the hidden field, not the checkbox itself, as the hidden field is always posted.

Sam
  • 6,167
  • 30
  • 39
  • 2
    Hi Sam, could you please give more information on the "konown problem"? KB or link? I have a checkbox with missing "uncheck"; enabled==True, checked=false when leaving OnInit; enabled==True, checked=true when entering Page_load; But it is unchecked from browser, and it work as expected if there was another unrelease asyncpostback happened before. – Dennis C Oct 15 '09 at 09:38
  • Thanks, I was facing the same issue. Stumbled upon this post after a lot of investigations from myside, and finally came to know that its a known issue in ASP.NET :) Solution worked for me. – Nirman Oct 08 '18 at 10:27
18

I had a similar problem to this where the Checked property of the CheckBox object was not being updated correctly, to get the actual posted value you can check:

Request.Form[CheckBox1.UniqueID]

it will be 'on' if the box is checked and null if not.

Syed Farjad Zia Zaidi
  • 3,302
  • 4
  • 27
  • 50
Kurt Vining
  • 181
  • 1
  • 2
  • 2
    (6 years later...) It's odd. My ASP.Net app is "sometimes" reporting the wrong Checked value for my ASP.Net Checkbox, and *sometimes* this suggestion does work... other times it throws an exception as this control's value isn't found in the Request.Form value... – Mike Gledhill Oct 17 '17 at 07:29
  • This just worked for me when nothing else would! that value is "on" but the checked property is "false". – Jamie M. Aug 13 '19 at 21:43
1

Since you're already using Javascript to manipulate the controls state in the browser, I suggest you just disable the checkbox on the page load event in stead. Then your postbacks will work just fine...

<head>

  <script type="text/javascript">
    function buttonClick() {
      var cb = document.getElementById('<%= CheckBox1.ClientID %>');
      cb.disabled = false;
      cb.parentNode.disabled = false;
    }    
  </script>

</head>
<body onload="document.getElementById('<%= CheckBox1.ClientID %>').disabled = true;">
  <form id="form1" runat="server">
  <div>
    <asp:checkbox id="CheckBox1" runat="server" checked="true" />
    <asp:button id="Button1" runat="server" text="Button" onclientclick="buttonClick(); return false;" />
    <asp:button id="Button2" runat="server" text="Button2" onclick="button2Click" />
  </div>
  </form>
</body>
Jakob Gade
  • 12,319
  • 15
  • 70
  • 118
  • I did something similar to that in my project. It's not very good for security however. If the user disable Javascript, he'd basically be able to check the box and press Save without hindering. – Simon Dufour Oct 11 '11 at 19:49
1

I may not understand the problem correctly, but can't you just use the Form from the Request to retrieve the CheckBox value? So in button2Click() in the code behind file you'd do this:

Request.Form[CheckBox1.UniqueID]
Syed Farjad Zia Zaidi
  • 3,302
  • 4
  • 27
  • 50
goku_da_master
  • 4,257
  • 1
  • 41
  • 43
  • 2
    No, browsers are not required to postback values of disabled form elements. –  Oct 24 '12 at 10:16
0

You can use something like this.

if ((Request.Params["Checkbox1"] ?? "").ToString() == "on")

If it isn't checked it won't pass it in the first place, but this should account for that.

Underground
  • 127
  • 5
0

Here is an alternative solution if you need to avoid recompiling your source code. It just enables the checkbox for a split second before submitting the form. 1: Add the following parameter to your submit button:OnClientClick="EnableCheckbox()" 2: Add this simple function somewhere on the page to enable it

<script> 
       function EnableCheckbox() {
            document.getElementById("<%=chkMyCheckbox.clientID  %>").disabled = false;
        } 
</script>