Disabled checkbox loses value on postback

  1. #1

    Disabled checkbox loses value on postback

    Ext.Net 1.2
    .Net 4.0

    To reproduce using the code below, press button 1.) then 2.)

    The problem might be in the LoadPostData method of CheckBox.cs


    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="cbox.aspx.vb" Inherits="cbox" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    
    <!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">
        <ext:ResourceManager ID="ResourceManager1" runat="server">
        </ext:ResourceManager>
        <div style="margin-left: 10px;">
            <ext:Button runat="server" ID="btn1" Text="1.) Check & Set Disabled" AutoPostBack="true" OnClick="setProperties">
            </ext:Button>
            <ext:Button runat="server" ID="btn2" Text="2.) Do Post" AutoPostBack="true" OnClick="doPost">
            </ext:Button>
            <ext:Checkbox runat="server" ID="chk1">
            </ext:Checkbox>
        </div>
        </form>
    </body>
    </html>
    
    Partial Class cbox
        Inherits System.Web.UI.Page
    
        Protected Sub doPost(sender As Object, e As System.EventArgs)
    
            Response.Write("chk1.Checked = " & chk1.Checked)
    
        End Sub
    
    
        Protected Sub setProperties(sender As Object, e As System.EventArgs)
         
            chk1.Disabled = True
            chk1.Checked = True
          
        End Sub
    End Class

    Possible Fix:

    In Form\Checkbox.cs
    
    
     protected override bool LoadPostData(string postDataKey, NameValueCollection postCollection)
            {
                this.HasLoadPostData = true;
    
                string val = postCollection[this.UniqueName];
    
                this.SuspendScripting();
                this.RawValue = val;
    
                try
                {
                    bool result = this.Checked != val.IsNotEmpty();
                    if (this.Checked && this.Disabled)
                    {
                        this.Checked = true;
                    }
                    else { 
                     this.Checked = val.IsNotEmpty();
                    }
                   
                    return result;
                }
                catch
                {
                }
                finally
                {
                    this.ResumeScripting();    
                }
    
                return true; 
            }
    Last edited by zach; Nov 07, 2011 at 11:39 PM. Reason: Added possible fix
  2. #2
    Hi,

    Thanks for the report. We will investigate.

    Here is a cleaned-up example.

    Example

    <%@ Page Language="C#" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void SetProperties(object sender, EventArgs e)
        {
            this.Checkbox1.Disabled = true;
            this.Checkbox1.Checked = true;
        }
        
        protected void DoPost(object sender, EventArgs e)
        {
            this.Label1.Text = this.Checkbox1.Checked.ToString();
        }
    </script>
     
    <!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>Ext.Net Example</title>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
            <ext:Button 
                runat="server" 
                Text="1.) Check & Set Disabled" 
                AutoPostBack="true" 
                OnClick="SetProperties" />
            <ext:Button 
                runat="server" 
                Text="2.) Do Post" 
                AutoPostBack="true" 
                OnClick="DoPost" />
            <ext:Checkbox ID="Checkbox1" runat="server" />
            <ext:Label ID="Label1" runat="server" />
        </form>
    </body>
    </html>
  3. #3
    According W3C standard, disabled controls are not submittable (browser excludes such controls from the post)
    Ext.Net includes disabled controls to direct request (it can be deactivated by SubmitDisabled property of ResourceManager)
    But if you use AutoPostBack then browser manages submitting (not Ext.Net)

    So, it is desired behaviour, not a bug
  4. #4
    Hi Vladimir,

    The behavior according to the spec is correct, but seems like a bug for Ext.net. Viewstate should be persisting the value, not the POST.

    Here are a couple more considerations:

    • The ext:checkbox's behavior does not match the behavior of the asp:checkbox.
    • Even other ext controls do not work this way.


    Updated examples below.

    
    <%@ Page Language="C#" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
     
    <script runat="server">
        protected void SetProperties(object sender, EventArgs e)
        {
           
            this.Checkbox1.Checked = true;
            this.Checkbox1.Disabled = true;
    
            this.TextField1.Text = "This will persist across postbacks even when disabled.";
            this.TextField1.Disabled = true;
    
            this.AspCheckbox1.Checked = true;
            this.AspCheckbox1.Enabled = false;
        }
         
        protected void DoPost(object sender, EventArgs e)
        {
            this.Label1.Text = "EXT Checkbox = " + this.Checkbox1.Checked.ToString();
    
            this.Label2.Text = "ASP Checkbox = " + this.AspCheckbox1.Checked.ToString();
        }
    </script>
      
    <!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 id="Head1" runat="server">
        <title>Ext.Net Example</title>
    </head>
    <body>
        <form id="Form1" runat="server">
            <ext:ResourceManager ID="ResourceManager1" runat="server" />
            <ext:Button ID="Button1"
                runat="server"
                Text="1.) Check & Set Disabled"
                AutoPostBack="true"
                OnClick="SetProperties" />
            <ext:Button ID="Button2"
                runat="server"
                Text="2.) Do Post"
                AutoPostBack="true"
                OnClick="DoPost" />
          
           <ext:CheckBox ID="Checkbox1" runat="server" />
           <ext:Label ID="Label1" runat="server" />
           <br />
           <ext:TextField ID="TextField1" runat="server"></ext:TextField>
           <asp:CheckBox ID="AspCheckbox1" runat="server" />
             <ext:Label ID="Label2" runat="server" />
           
        </form>
    </body>
    Last edited by zach; Nov 08, 2011 at 4:57 PM.
  5. #5
    Unchecked checkbox doesn't submit own value therefore we cannot distiniguish cases: disabled checkbox is not submitted or unchecked checkbox (if checkbox is absent in submit data then we have to set false for Checked). As you understood we cannot rely on ViewState in this case
  6. #6
    Quote Originally Posted by Vladimir View Post
    As you understood we cannot rely on ViewState in this case
    Thanks Vladimir. I'm trying to understand... The check and disabled state are being set on the server-side, so viewstate should be aware I think? Do you disagree? Also, from a end-user-developer perspective, I would think that people would want the same behavior as the regular asp.net control.
  7. #7
    I think it's not related to ViewState, because it behaves the same way with disabled ViewState.

    We think that they just do nothing with a disabled (server side Enabled="false") checkbox.

    Generally speaking, an <asp:CheckBox> have the same problem.

    1. Set up:
    <asp:Checkbox ID="AspCheckbox" runat="server" Checked="true" />
    2. Disable it on client side:
    Ext.fly("AspCheckbox").dom.disabled = true;
    3. Cause a post back.

    An <asp:CheckBox> will be enabled and lost its checked status. I guess it's undesired behavior for all developers.

    As well, a disabled on server side checkbox can be enabled on client side, so, it also causes issues.

    We decided to don't consider .Disabled status in a LoadPostData handler, because client side checkbox enabling/disabling often appear in the Ext.Net/ExtJS world and it cause more issues if we would consider it.

    A 100% possible solution would be using a DirectEvent/DirectMethod and passing a checkbox's value and, probably, its disabled state manually as an extra parameter. In this case you can disable a checkbox's value submitting, setting up SubmitValue="false" for an <ext:Checkbox>.

Similar Threads

  1. Replies: 7
    Last Post: Sep 20, 2010, 10:06 PM
  2. [CLOSED] Adding Checkbox items to checkbox group during a postback?
    By vedagopal2004 in forum 1.x Legacy Premium Help
    Replies: 19
    Last Post: Feb 05, 2010, 10:13 AM
  3. [CLOSED] Disabled Checkbox is always false and shows enabled true.
    By Sharon in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Nov 12, 2009, 4:10 AM
  4. [CLOSED] Gridpanel Checkbox Postback
    By LeeTheGreek in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Jul 14, 2009, 3:34 AM
  5. [CLOSED] Can't read value from disabled checkbox
    By Pablo in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Jul 02, 2009, 2:05 PM

Tags for this Thread

Posting Permissions