Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Checkbox value at postback is wrong?

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.

like image 750
Anthony Avatar asked Oct 06 '09 04:10

Anthony


3 Answers

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.

like image 184
Sam Avatar answered Sep 20 '22 18:09

Sam


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.

like image 38
Kurt Vining Avatar answered Sep 20 '22 18:09

Kurt Vining


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>
like image 24
Jakob Gade Avatar answered Sep 24 '22 18:09

Jakob Gade