Faraz Shah Khan

MCP, MCAD.Net, MCSD.Net, MCTS-Win, MCTS-Web, MCPD-Web

Mutually exclusive checkbox in GridView using Javascript

I response to one of my blog post entry I received few queries regarding mutually exclusive checkbox within GirdView, meaning if there are two checkboxes in a row only one can be selected at a time. If checkbox1 is selected and you select checkbox2 then checkbox1 should be deselected. Following javascript and html can be used.

<script type="text/javascript">
        function checkMutuallyExclusive(target)
        {
            document.getElementById(target).checked = false;
        }
</script>
.
.
.
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" Width="400px">
        <Columns>
            <asp:TemplateField>
                <AlternatingItemTemplate>
                    <asp:CheckBox ID="cbStatus1" runat="server" />
                </AlternatingItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="cbStatus1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                AlternatingItemTemplate>
                    <asp:CheckBox ID="cbStatus2" runat="server" />
                </AlternatingItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="cbStatus2" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
</asp:GridView>

Code Behind:

----------------------------------.vb file if VB.Net is the language ------------------------------------

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            DirectCast(e.Row.FindControl("cbStatus1"), CheckBox).Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" & DirectCast(e.Row.FindControl("cbStatus2"), CheckBox).ClientID & "')")
            DirectCast(e.Row.FindControl("cbStatus2"), CheckBox).Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" & DirectCast(e.Row.FindControl("cbStatus1"), CheckBox).ClientID & "')")
        End If
End Sub


----------------------------------.cs file if C#.Net is the language ------------------------------------

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if ((e.Row.RowType == DataControlRowType.DataRow)) {
        ((CheckBox)e.Row.FindControl("cbStatus1")).Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" + ((CheckBox)e.Row.FindControl("cbStatus2")).ClientID + "')");
        ((CheckBox)e.Row.FindControl("cbStatus2")).Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" + ((CheckBox)e.Row.FindControl("cbStatus1")).ClientID + "')");
    }
}

Comments

Mohammed Imran said:

Thank You so much Faraz brother.

It solved my problem :)

I appreciate your efforts and quick response.

Thanks once again...

Regards,

Mohammed Imran

# August 2, 2008 7:27 AM

Ajay Sharma said:

what will be the changes if we are dealing with 4 checkboxes within a same row??

# August 10, 2008 4:29 PM

farazsk11 said:

@Ajay Sharma

Hi,

Well you can modify the javascript like this

<script type="text/javascript">

       function checkMutuallyExclusive(target1, target2, target3)

       {

           document.getElementById(target1).checked = false;

           document.getElementById(target2).checked = false;

           document.getElementById(target3).checked = false;

       }

</script>

your code behind can be like this....

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)

{

   CheckBox chk1 = (CheckBox)e.Row.FindControl("cbStatus1");

   CheckBox chk2 = (CheckBox)e.Row.FindControl("cbStatus2");

   CheckBox chk3 = (CheckBox)e.Row.FindControl("cbStatus3");

   CheckBox chk4 = (CheckBox)e.Row.FindControl("cbStatus4");

   if ((e.Row.RowType == DataControlRowType.DataRow)) {

       chk1.Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" + chk2.ClientID + "','" + chk3.ClientID + "','" + chk4 + "')");

       chk2.Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" + chk1.ClientID + "','" + chk3.ClientID + "','" + chk4 + "')");

chk3.Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" + chk1.ClientID + "','" + chk2.ClientID + "','" + chk4 + "')");

chk4.Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" + chk1.ClientID + "','" + chk2.ClientID + "','" + chk3 + "')");        

   }

}

I will try to provide a generalized version later but as of now this will fulfill your requirement.

# August 10, 2008 4:47 PM

Ajay Sharma said:

thank you so much for a quick reply..but can you please provide me the vb.net code, i wil appriciate that.

Thanks

Ajay

# August 10, 2008 5:53 PM

farazsk11 said:

Javascript will remain the same, where as the equivalent VB.net code is:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

       If (e.Row.RowType = DataControlRowType.DataRow) Then

Dim chk1 as CheckBox = CType(e.Row.FindControl("cbStatus1"), CheckBox)

Dim chk2 as CheckBox = CType(e.Row.FindControl("cbStatus2"), CheckBox)

Dim chk3 as CheckBox = CType(e.Row.FindControl("cbStatus3"), CheckBox)

Dim chk4 as CheckBox = CType(e.Row.FindControl("cbStatus4"), CheckBox)

  chk1.Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" & chk2.ClientID & "','" & chk3.ClientID & "','" & chk4 & "')")

      chk2.Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" & chk1.ClientID & "','" & chk3.ClientID & "','" & chk4 & "')")

chk3.Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" & chk1.ClientID & "','" & chk2.ClientID & "','" & chk4 & "')")

chk4.Attributes.Add("onclick", "javascript:checkMutuallyExclusive('" & chk1.ClientID & "','" & chk2.ClientID & "','" & chk3 & "')")

End If

End Sub

# August 10, 2008 6:04 PM

mcru said:

Perfect, I've been trying to do this for a LONG time. Works exactly how I need it to.

# September 8, 2008 3:20 PM

Seshu said:

Hi Shah,

Great work. Thanks!

# September 9, 2008 10:34 AM

Helder said:

Great work, I've do this but my solution d'ont work so perfect that this.

Tanks!

# October 7, 2008 9:00 AM

Steve said:

Nice code!  I did the same thing with much more coding.  I think I will replace it all with yours.  Thanks!

# October 10, 2008 2:39 PM

Anitta said:

Hi,

Same thing for Windows.

# December 2, 2008 7:24 AM

Marc said:

great solution!

thanx a lot!

Marc

# January 20, 2009 10:49 AM

jalsa said:

hello all,

when i tried this i am getting an error saying getelementbyID is null.

please help.

thanks,

jalsa

# March 1, 2009 3:59 PM

... said:

Interessante Informationen.

# March 3, 2009 5:16 AM

farazsk11 said:

@jalsa:

Are you sure you are using the same code or you are passing ClientID of the control properly. If everything is correct the code sample should work and shouldn't return null on getElementById.

One more thing are you using AJAX?

# March 4, 2009 1:00 AM

... said:

Sehr wertvolle Informationen! Empfehlen!

# March 14, 2009 7:25 PM

Lee said:

Excellent solution. Good stuff

# July 16, 2009 12:20 AM

GMann said:

Works like a champ.

Thanks !

# October 27, 2009 2:20 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)