We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Posting disabled checkbox value in ASP

geoenvi
geoenvi asked
on
Medium Priority
1,090 Views
Last Modified: 2012-08-13
I have two checkboxes chkboxA and chkboxB.
If someone clicks on chkboxA, I need to check the chkboxB and disable it so that chkboxB can be unchecked only by unchecking the chkboxA.  If chkboxA is unchecked then the users are free to check/uncheck the chkboxB. (Code below that works fine to do this.

However, when I try to submit the form in ASP, the disabled chkbox value does not get posted.  I have since understood that ASP does not post disabled values during submit.  What are my options here? Any other way to make the chkbox readonly/disabled from Javascript?




I used the following code that works fine to do this :
******************************************************************************************
function disablechkboxB()
{
    if (document.NewuserForm.chkboxA.checked==true)
    {
   
    document.getElementById('td1').style.color='Gray';
    document.NewuserForm.chkboxB.checked=true;
   document.NewuserForm.chkboxB.readonly=true;
    }
    else
    {
     document.getElementById('td1').style.color='Black';
    document.NewuserForm.chkBoxB.checked=false;
    document.NewuserForm.chkBoxB.disabled=false;
   
    }
    return true;
}
*******************************************************************************************
Comment
Watch Question

Systems architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Commented:
Browsers don't post disabled form controls in HTML documents - the ASP is irrelevent.

Use readonly instead of disabled. (Disabled really does mean disabled)
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Of course you can put that single statement also inside your form validation function if onLoad is already used by that validation function call.
Enable all the checkboxes on submission, eg

<script>
function enable(form){
form.chkBoxA.disabled=false;
form.chkBoxB.disabled=false;
return true
}
</script>

<form name=f onsubmit='enable(this)'>
<input type=checkbox name=chkboxA>
<input type=checkbox name=chkboxB>
</form>
Hmmm...Zvonko that's a good idea. :)
Wow...4 posts in the same minute...that must be a rare occurence. ;)
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
I think hidden field is the better idea.
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Hello geoenvi,

in your profile I saw that you regulary give grading B and C
Don't you know that grading A does not cost you more :)
And by the way, we experts get more expert points when you grade A.
So please be so kind :-)
CERTIFIED EXPERT

Commented:
>>>However, when I try to submit the form in ASP, the disabled chkbox value does not get posted.
even is not disabled and not checked value does not get posted.

>>>Code below that works fine to do this

here some modification ;)




<script>
function disablechkbox(obj,f)
{
      obj.parentNode.style.color=obj.checked?"gray":"black";
      obj.form.elements[f].disabled=obj.checked;
      return true;
}
</script>
<table cellspacing=0 cellpadding=0 border=1>
      <form  action=http://www.google.com/search/? name=NewuserForm >
<tr>      <td>
            Select Type:<br>
            A<input type=checkbox value=A name=chkboxA onclick=disablechkbox(this,"chkboxB")>
            B<input type=checkbox value=B name=chkboxB onclick=disablechkbox(this,"chkboxA")>
      </td>
</tr>
<tr>
      <td><input type=submit></td>
</tr>      
      </form>
</table>

Author

Commented:
puffball,
Tried your method before and also tried it now.  Here is what is happening. The click event for ChkBoxB where you are assigning the value to a hidden variable is not being fired if the check is placed from Javascript (function disablechkboxB())

Since the event is not being fired, the value is not being assigned to the hidden field.

Zvonk,
Your suggestion does look appealing.  I will try that in the next few mins.  Something tells me that I can set the disabled (not readonly) property back to true during form submit, I should be good to go.  If it works, I have no problems giving you an A.  Thanks for all your help.  I will get back in a few....


Oops, sorry, I notice I made a mistake in the function. If you are still interested in doing it my way, here is the corrected function:

function disablechkboxB()
{
    if (document.NewuserForm.chkboxA.checked==true)
    {
    document.getElementById('td1').style.color='Gray';
    document.NewuserForm.chkboxB.checked=true;
   document.NewuserForm.chkboxB.disabled=true;
   document.NewuserForm.chkboxBValue.value=true;
    }
    else
    {
     document.getElementById('td1').style.color='Black';
    document.NewuserForm.chkboxB.checked=false;
    document.NewuserForm.chkboxB.disabled=false;
   document.NewuserForm.chkboxBValue.value=false;    
    }
    return true;
}

You may also want to set the value of the hidden field to false as a default.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.