Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to make two checkboxes mutually exclusive with the help of javascript in asp.net...any one help..
Posted
Updated 6-Jan-13 21:47pm
v2

two checkboxes mutually exclusive with the help of javascript in asp.net
Option 1: Why use Checkbox if so? Use RadioButtons (suggestible)
Option 2: If you really want to use checkbox, then try following:
JavaScript
<script type="text/javascript">  
      $(document).ready(function(){  
            //toggle all checkboxes in toggleContainer div  
            $('#toggleContainer :checkbox').bind('change',function(){  
                var thisClass=$(this).attr('class');  
                if($(this).attr('checked'))  
                {  
                    $('#toggleContainer :checkbox.'+thisClass+":not(#"+this.id+")").removeAttr('checked');      
                }  
                else  
                {  
                    $(this).attr('checked','checked');                            
                }  
            });  
        });  
    </script> 

Above code snippet is mentioned in detail here: jQuery Mutually exclusive checkboxes in asp.net [^]

Option 3 & 4:
Refer: How can I make a group of checkboxes mutually exclusive?[^]
MS ASP.NET Creating Mutually Exclusive Checkboxes (C#)[^]
 
Share this answer
 
Please check this.

http://www.asp.net/web-forms/tutorials/ajax-control-toolkit/mutuallyexclusivecheckbox/creating-mutually-exclusive-checkboxes-cs[^]

or you can use following code

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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">
 <script src="http://code.jquery.com/jquery-1.8.3.min.js" type="text/javascript"></script>
    <title></title>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#chkbox input:checkbox").click(function() {               
                checkState = $(this).attr('checked');
                $('#chkbox input:checkbox').each(function() {
                    $(this).attr('checked', false);
                });
                $(this).attr('checked', true);
            });
        });      
    </script> 
</head>
<body>
    <form id="form1" runat="server">
    <div id="chkbox">
    <asp:CheckBox ID="CheckBox1" runat="server" />One<br />
        <asp:CheckBox ID="CheckBox2" runat="server" />Two<br />
        <asp:CheckBox ID="CheckBox3" runat="server" />Three<br />
        <asp:CheckBox ID="CheckBox4" runat="server" />Four<br />
    </div>
    </form>
</body>
</html>
 
Share this answer
 
v2
JavaScript
<script language="javascript" type="text/javascript">
function CheckUncheck(chk1, chk2) {
var chkbox1 = document.getElementById(chk1);
var chkbox2 = document.getElementById(chk2);
chkbox2.checked = !chkbox1.checked;
}
</script>

ASP.NET
<asp:checkbox id="CheckBox1" runat="server" />
<asp:checkbox id="CheckBox2" runat="server" />


Here is the code behind:
C#
if (!IsPostBack)
{
CheckBox1.Attributes.Add("onclick", "CheckUncheck('" + CheckBox1.ClientID + "','" + CheckBox2.ClientID + "');");
CheckBox2.Attributes.Add("onclick", "CheckUncheck('" + CheckBox2.ClientID + "','" + CheckBox1.ClientID + "');");
}
 
Share this answer
 
v2
The best solution is not to use a check box - use a radio button instead: http://www.w3schools.com/aspnet/control_radiobutton.asp[^]

Users are familiar with radio buttons being "one of these", but expect check boxes to allow multiple selections, not be mutually exclusive
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900