Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

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

Posted on Tuesday, March 7, 2006 12:05 AM | Back to top


Comments on this post: DeSeleting All CheckBoxes When DeSelect One Item CheckBox

# re: DeSeleting All CheckBoxes When DeSelect One Item CheckBox
Requesting Gravatar...
Hi,

Sorry azam but this code is not working for usercontrols, i.e if grid is used in usercontrol, the id of each checkbox is different so this code fails...any way around....
Left by Abhishek on Jul 11, 2006 5:59 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net