views:

36

answers:

1

Hi all i am having two check box controls in my gridview what i need is if i checked a check box the other should be get enabled and if i uncheck it should get disable is there any way to do it.

I write this but i am not getting the required one

 <script type="text/javascript">
  function checkboxClick(checked, boxId) {
 var childCheckbox = document.getElementById(boxId);
 childCheckbox.disabled = !checked;

  if(childCheckbox.disabled) //uncheck when disabled
     childCheckbox.checked = false;
}


</script>

  <asp:CheckBox ID="CheckBox1" runat="server" OnClientClick ="checkboxClick(this.checked);" />
    <asp:CheckBox ID="boxId" runat="server" Enabled="false" /></div>
+1  A: 

Basically, what you want is something like this:

function checkboxClick(checked) {
   document.getElementById('childCheckbox').disabled = !checked;
}

<input type="checkbox" onclick="checkboxClick(this.checked);" />
<input type="checkbox" id="childCheckbox" />

Now, if they're ASP.NET controls, <asp:CheckBox />, you will probably want to edit checkboxClick so that it also takes the ClientID of the checkbox to enable/disable.

<asp:CheckBox runat="server" id="cb1" />
<asp:CheckBox runat="server" id="cb2" />

var cb2 = FindControl("cb2");
((CheckBox) FindControl("cb1")).OnClientClick = "checkboxClick(this.checked, '" + cb2.ClientID + "');";

EDIT

Accommodating further requirements as per comments:

function checkboxClick(checked, boxId) {
   var childCheckbox = document.getElementById(boxId);
   childCheckbox.disabled = !checked;

   if(childCheckbox.disabled) //uncheck when disabled
      childCheckbox.checked = false;
}

<asp:CheckBox runat="server" id="cb1" />
<asp:CheckBox runat="server" id="cb2" Enabled="false" /> <!-- disable on load -->
David Hedlund
Need one more help when i click on second check box and if i deselect the first check box the 2nd check box should be unchecked .
Dorababu
And also how can i make my second check box as non selectable initially it should get enabled only after checking the First check box
Dorababu
@Dorababu: alright, see my edits.
David Hedlund
@David : Hi i have updated my question with the script given by you but i am not getting the proper one i required can you please tell what's wrong in that
Dorababu
@Dorababu: in .NET `onClick` of a server control relates to the .NET event. to call a javascript function, you need to use `OnClientClick` and you need to pass the `ClientID` of the second checkbox, as I've done in my example code above.
David Hedlund
Even though i am unable to enable the second check box
Dorababu
@Dorababu: in the code that is currently posted, you have not made any of the corrections i pointed out in my last comment. if you have made any changes, please post your updated code, otherwise, see my previous comment.
David Hedlund
I made the changes i just used OnClientClick as you said
Dorababu
Alright, so now you're using `OnClientClick` but you're still not passing the `ClientID` of the second checkbox to your javascript function the way I've done in my example, just above the *edit*. That's C# code. If you'll be databinding your grid in the ASPX you can probably use some kind of `DataBinder.Eval` method to achieve the same, I haven't really looked that much into databinding in the ASPX page, and I really don't think it's worthwhile :) If things still aren't working, you'll need to provide more code to show how you're databinding your gridview.
David Hedlund
I just checked your code by using the check boxes on normal web page not in the gridview
Dorababu