• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

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?
0
WhatupE
Asked:
WhatupE
  • 4
  • 2
1 Solution
 
WhatupEAuthor Commented:
The enable/disable is handled by javascript.  There is no postback.
0
 
praneethaCommented:
can u post the code (javascript and codebehind)

0
 
raterusCommented:
This is caused because when you disable a control in javascript, it doesn't post it's value when the form submits.  asp.net never gets the value, so it has to rely on the viewstate.  Some things  you can do are:

-try hiding the first checkbox if they check the second one, not disabling it
-Have your "real" checkboxes that asp.net looks at hidden to the user, the viewable checkboxes only update the values of these hidden checkboxes.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
WhatupEAuthor Commented:
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;
      
}
0
 
WhatupEAuthor Commented:
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
0
 
raterusCommented:
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
0
 
WhatupEAuthor Commented:
thanks, michael
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now