Search code examples
c#checkboxcode-behind

C# javascript enable/disable checkboxes


I have some checkboxes dynamically created in C# codebehind. Let's say two columns. This works fine.

I want the checkboxes in the left column to enable/disable those in the right upon check. I do this by attaching attributes to the checkboxes in codebehind that run javascript to do the enabling/disabling. This works great.

Here's the javascript:

function SetPrimaryStatus(target, senders) {
    var dis = "disable";
    var arr = senders.split(",");
    for (var i = 0; i < arr.length; i++) {
        if (document.getElementById(arr[i]).checked == true) {
            dis = "";
            break;
        }
    }
    document.getElementById(target).disabled = dis;
    if (dis == "disable") document.getElementById(target).checked = false;
}

I thought I was home free...I just have to disable the right column checkboxes upon creation in the codebehind (CheckBox.Enabled = false;). Ok.

For whatever reason now, when I check the left checkboxes, the right ones do nothing. I've set alerts in the javascript and they always come up...and things appear to be set like they should. When I remove the CheckBox.Enabled = false; in codebehind, it works again.

In fact, I can start some of the left checkboxes with a check and the corresponding right checkbox I leave enabled. When I run the set(s) that starts enabled works perfect but the set(s) that start with the right checkbox disabled do not.

Is there something other than Enabled that I should be setting in codebehind to start the checkboxes in a disabled state that the javascript can then change? Is it a matter of true/false compared to "disable"? I've tried both.


Solution

  • I found the answer here: http://forums.asp.net/t/982967.aspx/1.

    I had to add document.getElementById(target).parentElement.disabled = dis; to my javascript function. All works as expected now. Thanks for everyone's suggestions.