Pages

Wednesday, May 4, 2011

Checking All CheckBoxes in a GridView By JavaScript


Checking All CheckBoxes in a GridView 

In the code you can  checking All CheckBoxes in a GridView follwing is the image of interface .


Using the Client Side Javascript checking the header checkbox would check all checkboxes in the GridView and after unchecking the
header checkbox would uncheck all checkboxes in the GridView .


Copy the following javascript code and put it inside the head tag .

Javascript Code for ASP.Net GridView Check All Checkbox

<script type="text/javascript">
  function chkAllCheckbox(obj) {
            var gv = document.getElementById('<%=GVMain.ClientID %>');
                for (var i = 0; i < gv.all.length; i++) {
                     var node = gv.all[i];
                     node.checked = obj.checked;               
                   }          
              }
 </script>

HTML Code for ASP.Net GridView Check All Checkbox

<asp:GridView ID="GVMain" runat="server" AutoGenerateColumns="False" BackColor="White"
            BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
            <RowStyle ForeColor="#000066" />
            <Columns>
                <asp:TemplateField HeaderText="Select All">
                    <HeaderTemplate>
                        <asp:CheckBox ID="chkSelectAll" onClick="javascript:chkAllCheckbox(this);" Text="Select All" runat="server" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkSelect" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <asp:Label ID="lblID" Text='<%# Eval("ID") %>' runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lblName" Text='<%# Eval("Name") %>' runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        </asp:GridView>


Related Other posts

10 comments:

  1. it is very helpful.Thank you

    ReplyDelete
  2. Nice post...But how will i bind the data from database to my grid???Post the corresponding code..

    ReplyDelete
  3. for gridview bind code is please visit http://hamidseta.blogspot.com/2011/05/nested-gridview-in-aspnet.html

    ReplyDelete
  4. Nice post ..it is useful .

    ReplyDelete
  5. nice work...keep rocking

    ReplyDelete
  6. Nice Post, It is working fine in IE but it is not working in Google Chrome, Let me know what i have to do now,

    thanks,
    surya

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete