0
votes

I have this Code,

  <asp:UpdatePanel runat="server">
    <ContentTemplate>   
        <asp:GridView ID="gvTestDetails" runat="server" AutoGenerateColumns="false" OnDataBound="gvTestDetails_DataBound" EnableModelValidation="true">    
            <Columns>
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:CheckBox ID="cbSelectRecord" runat="server" AutoPostBack="true"  OnCheckedChanged="cbSelectRecord_CheckedChanged"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

What I want is,When I check one of the CheckBox in Row of the GridView all other CheckBoxes get un-Checked.

3
"I have this" Don't post images but code. - Tim Schmelter
Plz code so we can see what you have tried. - Dean.DePue
Why you do it on server side but not on client side? - Adil
@timschmelter ... I m sorry for this but i tried pasting the code but the code was getting displayed partially..so i have to post it like this only.. - maitiest
Use the "code"-button to format the selected code properly. - Tim Schmelter

3 Answers

1
votes

Why dont you just use the Mutually Exclusive CheckBox from the Ajax Controll Toolkit ?

Just add it below your CheckBox:

<ajaxToolkit:MutuallyExclusiveCheckboxExtender runat="server"
    ID="RandomID"
    TargetControlID="cbSelectRecord" 
    Key="AllwaysSameID" />

Note: You need to include a scriptmanager and AjaxControllToolkit in your project then. But this isnt bad because the AjaxControllToolkit offers alot of great stuff.

0
votes

You can loop all rows in the CheckedChanged event and uncheck all others:

protected void cbSelectRecord_CheckedChanged(object sender, EventArgs e)
{
    CheckBox cb = (CheckBox) sender;
    if(cb.Checked)
    {
        foreach(GridViewRow row in this.gvTestDetails.Rows)
        {
            CheckBox rowRb = (CheckBox) row.FindControl("cbSelectRecord");
            if(rowRb != cb)
              rowCb.Checked = false;
        }
    }
}
0
votes

use JQuery

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript">
function checkRecAll() {
var len = document.forms['form1'].elements.length;
var fields = document.forms['form1'].elements;
var chkStatus = document.all("GridView1_chkselall").checked
for (i = 0; i < len; i++) {
if ((fields[i].name.indexOf('chkrecords') != -1)||     (fields[i].name.indexOf('chkrecords') != -1)) {
if (chkStatus == true) {
if (document.all(fields[i].name).disabled == false) {
document.all(fields[i].name).checked = true;
}
}
else {
document.all(fields[i].name).checked = false;
}
}
}
}
</script>

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox name="chkselall" ID="chkselall" runat="server" onclick="javascript:checkRecAll()">
                    </asp:CheckBox>Select All
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="chkrecords" runat="server"></asp:CheckBox>
                </ItemTemplate>
            </asp:TemplateField>
             <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Age" HeaderText="Age" />
        </Columns>
    </asp:GridView>