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 + "')");
    }
}

16 Comments

  • Thank You so much Faraz brother.
    It solved my problem :)
    I appreciate your efforts and quick response.

    Thanks once again...

    Regards,
    Mohammed Imran

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

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

  • 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

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

  • Hi Shah,

    Great work. Thanks!

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


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

  • hello all,
    when i tried this i am getting an error saying getelementbyID is null.
    please help.
    thanks,
    jalsa

  • Interessante Informationen.

  • @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?

  • Sehr wertvolle Informationen! Empfehlen!

  • Excellent solution. Good stuff

  • Works like a champ.

    Thanks !

  • thanks a lot!! solved my problem too!!

  • Excellent job..thanks ..

Comments have been disabled for this content.