DeSeleting All CheckBoxes When DeSelect One Item CheckBox

Ahhhh its 2:00 AM and me little sleepy! Anyway someone asked me on the forums.gridviewguy.com that how can I deselect all the checkboxes in the gridview control when a single checkbox is unchecked. Offcourse this is very easy if you are using and want this functionality to happen when header checkbox is checked and unchecked. But the request was that when any checkbox in the GridView is unchecked it should disable all the checkboxes.

Here is the code which does the same:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField HeaderText="AutoNumber">
                        <ItemTemplate>
                          <asp:CheckBox 
                          onclick="JavaScript:DeSelectCheckBoxes(this);"
                          ID="MyCheckBox" runat="server" />
                        </ItemTemplate>
                        <HeaderTemplate>
                            <asp:CheckBox
                            onclick="javascript:SelectAllCheckboxes(this);"
                            ID="chkAll" runat="server" />
                        </HeaderTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            
            
             function DeSelectCheckBoxes(spanChk) 
    {
        
// Added as ASPX uses SPAN for checkbox
    
var oItem = spanChk.children;
    var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
    xState=theBox.
checked;
    
    
// this means it is deselected
    
if(xState == false
    {
    elm=theBox.form.elements;
    
for(i=0;i<elm.length;i++)
    
if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)
    {    
        elm[i].
checked false;       
    }       
         
    } 
    } 

Hope it helps!

powered by IMHO 1.3

Print | posted @ Tuesday, March 07, 2006 12:05 AM

Twitter