Solved

How to re-enable check boxes after disabled?

Posted on 2011-09-12
10
249 Views
Last Modified: 2012-05-12
Hello Experts. I have a popup Asp.net web form with Check boxes on it. I am using Java Script. Basically if someone checks one of the check boxes, I need to disable the other remaining check boxes. If someone then Un-Checks the chosen check box, I need to re-enable all of the check boxes again. I figured out how to disable the remaining check boxes after the first one is checked. But how do I re enable all of the other check boxes again? Below is my Java Script method I am using. Attached is the full Asp.net web form which uses the Java Script.

For some reason I am not able to paste the Java Script in Here. Please

see see that attached Java script method named WhichOneChecked

 
JavaScript.txt
0
Comment
Question by:brgdotnet
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36527026
not really something to fix your code but you can start by
replacing : boolean allUnchecked=false;
with : var allUnchecked = false;

in your browser, right click on your page , choose view source and post it here
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 36527271
Yeah, but I still do not have a solution to the problem? I need to know how to re-enable the check boxes. So your answer didn't help me.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36527278
in your browser, right click on your page , choose view source and post it here
0
Technology Partners: 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!

 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 90 total points
ID: 36527286
Check this..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
  <head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('input.gridchk:checkbox').click(function(){
          if ($(this).is(':checked')) {
            $('input.gridchk:checkbox').attr('disabled', true);
            $(this).attr('disabled',false);
          }
          else 
            $('input.gridchk:checkbox').attr('disabled', false);
        });
    });
    </script>
</head> 
  <body> 
    <form id="form1" runat="server"> 
    <div> 
    
        <asp:CheckBox runat="server" ID="CheckBox1" onclick=" GoThroughGrid()" Text="One" /> 
    
    </div> 
    <asp:CheckBox ID="CheckBox2" runat="server" Text="Two" /> 
    <p> 
        <asp:CheckBox ID="CheckBox3" runat="server" Text="Three" /> 
    </p> 
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" OnRowDataBound="GridViewPage_OnRowDataBound" > 
        <Columns> 
        <asp:TemplateField HeaderText="Checked" itemstyle-cssclass="sectionContentItemC" ItemStyle-Width="50"> 
            <itemtemplate> 
            <asp:Literal id="literalMember" runat="server" visible="true" /> 
                <asp:Checkbox id="checkboxMember" onclick="WhichOneChecked()" runat="server" visible="true" CssClass="gridchk" /> 
            </itemtemplate> 
        </asp:TemplateField> 
    <asp:BoundField datafield="Name" headertext="Name" HeaderStyle-CssClass="hidden" itemstyle-cssclass="hidden" /> 
<%--        <asp:BoundField datafield="NameId" headertext="Customer" itemstyle-cssclass="sectionContentItem" sortexpression="CustomerName" /> 
--%>        </Columns> 
    </asp:GridView> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AirlinePayableDevConnectionString %>" 
        SelectCommand="SELECT [IsTestMode], [Name] FROM [Root]"> 
    </asp:SqlDataSource> 
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> 
    </form> 
</body> 
<html>

Open in new window

0
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 90 total points
ID: 36527287
at the onchange event of a checkbox, you need to check if this one is unchecked or not. If it is unchecked, then reenable all checkboxes like this

function enableAll()
{
             var grid = document.getElementById("<%= GridView1.ClientID%>");
             var inputs = grid.getElementsByTagName("input");
             for (var counter = 0; counter < inputs.length; counter++)
            {
                 if (inputs[counter].type.toLowerCase() == "checkbox" )
                 {
                     inputs[counter].disabled = false;
                 }
             }
}
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36527290
My mistake. Ignore above code and use below

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Popup.aspx.cs" Inherits="EnumStuff.Popup" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('input.gridchk:checkbox').click(function(){
          if ($(this).is(':checked')) {
            $('input.gridchk:checkbox').attr('disabled', true);
            $(this).attr('disabled',false);
          }
          else 
            $('input.gridchk:checkbox').attr('disabled', false);
        });
    });
    </script>
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    
        <asp:CheckBox runat="server" ID="CheckBox1" onclick=" GoThroughGrid()" Text="One" /> 
    
    </div> 
    <asp:CheckBox ID="CheckBox2" runat="server" Text="Two" /> 
    <p> 
        <asp:CheckBox ID="CheckBox3" runat="server" Text="Three" /> 
    </p> 
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" OnRowDataBound="GridViewPage_OnRowDataBound" > 
        <Columns> 
        <asp:TemplateField HeaderText="Checked" itemstyle-cssclass="sectionContentItemC" ItemStyle-Width="50"> 
            <itemtemplate> 
            <asp:Literal id="literalMember" runat="server" visible="true" /> 
                <asp:Checkbox id="checkboxMember" onclick="WhichOneChecked()" runat="server" visible="true" CssClass="gridchk" /> 
            </itemtemplate> 
        </asp:TemplateField> 
    <asp:BoundField datafield="Name" headertext="Name" HeaderStyle-CssClass="hidden" itemstyle-cssclass="hidden" /> 
<%--        <asp:BoundField datafield="NameId" headertext="Customer" itemstyle-cssclass="sectionContentItem" sortexpression="CustomerName" /> 
--%>        </Columns> 
    </asp:GridView> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AirlinePayableDevConnectionString %>" 
        SelectCommand="SELECT [IsTestMode], [Name] FROM [Root]"> 
    </asp:SqlDataSource> 
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> 
    </form> 
</body> 
</html>

Open in new window

0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36527294
Remove onclick="WhichOneChecked()" from <asp:Checkbox id="checkboxMember" onclick="WhichOneChecked()" runat="server" visible="true" CssClass="gridchk" />
0
 
LVL 13

Accepted Solution

by:
ansudhindra earned 120 total points
ID: 36527333
Hi,

Replace your function with the below code. and pass "this" in the onClick event.. like

<asp:Checkbox id="checkboxMember" onclick="WhichOneChecked(this)" runat="server" visible="true" />
 
function WhichOneChecked(ele){
    var strToDisplay ='';
    var inputs = document.getElementById('checkDiv').getElementsByTagName('input');
    if(ele.checked) {
    for(var i=0; i<inputs.length; i++) {
        if (inputs[i].type.toLowerCase() == "checkbox" && inputs[i].checked) 
            strToDisplay += "Checkbox index=" + i + " \n"; 
        else { 
            inputs[i].disabled = true; 
        } 
    }
    
    } else {
    for(var i=0; i<inputs.length; i++) {
        if (inputs[i].type.toLowerCase() == "checkbox") {
            inputs[i].disabled = false; 
        } 
    }
    
    }
}

Open in new window

0
 
LVL 13

Expert Comment

by:ansudhindra
ID: 36527341
sorry..
change line number 3 with the below line...

var grid = document.getElementById("<%= GridView1.ClientID%>").getElementsByTagName('input');
0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 36533970
Thanks so much
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Web Development 6 22
Add somewhat of a wildcard to your javascript 12 30
web page freezes after ajax post 7 44
Check input text, Number 6 30
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question