Link to home
Start Free TrialLog in
Avatar of WhatupE
WhatupE

asked on

Getting checkbox "checked" property when disabled by javascript

I have two checkboxes (check1, check2)

When check1 is checked, it remains enabled and enables check2.   When check2 is checked, it disables check1.  When I go to do my server side validations, I check if check1 is checked.  What is occurring is that check1.Checked is always returning false is my server side C# code-behind.  Why would this be happening, and more importantly, how can I fix it so that it returns the correct value?
Avatar of WhatupE
WhatupE

ASKER

The enable/disable is handled by javascript.  There is no postback.
can u post the code (javascript and codebehind)

ASKER CERTIFIED SOLUTION
Avatar of raterus
raterus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WhatupE

ASKER

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
      /// <summary>
      /// Summary description for WebForm1.
      /// </summary>
      public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.CheckBox CheckBox1;
            protected System.Web.UI.WebControls.Button Button1;
            protected System.Web.UI.WebControls.TextBox TextBox1;
            protected System.Web.UI.WebControls.Button Button2;
            protected System.Web.UI.WebControls.TextBox TextBox2;
            protected System.Web.UI.WebControls.CheckBox CheckBox2;
      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
                  this.CheckBox2.Attributes.Add("onclick", "CheckEmployment('" +
                        this.CheckBox1.ClientID + "','" +
                        this.CheckBox2.ClientID +
                        "');");
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Button1.Click += new System.EventHandler(this.Button1_Click);
                  this.Button2.Click += new System.EventHandler(this.Button2_Click);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            private void Button1_Click(object sender, System.EventArgs e)
            {
                  this.TextBox1.Text = this.CheckBox1.Checked.ToString();
            }

            private void Button2_Click(object sender, System.EventArgs e)
            {
                  this.TextBox2.Text = this.CheckBox1.Enabled.ToString();
            }
      }
}

function CheckEmployment(employChk, retireChk)
{
      var isChecked = document.getElementById(retireChk).checked;
      
      document.getElementById(employChk).disabled=isChecked;
      document.getElementById(employChk).checked=true;
      
}
Avatar of WhatupE

ASKER

raterus -

i tried setting the control value visible = false, but then the javascript blew up when i tried to set the checked value of the invisible control
I'm not talking about the server side visibility, that will make it so it will not even render to the client, not what you want. You must make it invisible in the html source that is sent to the client.

for example:
<input type="checkbox" id="myInvisibleCheckbox" style="visibility: hidden" />

--Michael
Avatar of WhatupE

ASKER

thanks, michael