Saturday, 28 June 2014

Code to Select All Checkbox in GridView in Asp.Net Using jQuery

6/28/2014 - By Pranav Singh 2

In this article I will show you how you can how to check all checkboxes in gridview using jquery in your asp.net application. In this we will put add select all checkbox in gridview header and when user click on this checkbox all the checkboxes of the gridview get selected automatically without post back on client side.


So for this article first we will create a new asp.net application and add a grid view control. In this gridview control we will  an itemtemplate and in the item template we will add a checkbox. Now we will write a jquery code to make the select other checkbox.

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script language="javascript" type="text/javascript">
        function Selectallcheckbox(val) {          
            if (!$(this).is(':checked')) {
                $('input:checkbox').prop('checked', val.checked);
            } else {
                $("#chkroot").removeAttr('checked');
            }
        }    
    </script>

In above code we have defined jquery library reference and added jquery code to select all checkboxed on click.

So here is the complete code for this article.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ProjectDemo_Asp.et.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Code to Select All Checkbox in GridView in Asp.Net Using jQuery</title>
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script language="javascript" type="text/javascript">
        function Selectallcheckbox(val) {          
            if (!$(this).is(':checked')) {
                $('input:checkbox').prop('checked', val.checked);
            } else {
                $("#chkroot").removeAttr('checked');
            }
        }    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EmptyDataText="There are no data records to display."
        BorderStyle="Solid">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="CheckBox2" runat="server" onclick="javascript:Selectallcheckbox(this);" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" CssClass="checkboxselection" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="author_name" HeaderText="NAME" />
            <asp:BoundField DataField="publisher_name" HeaderText="PUBLISHER NAME" />
            <asp:BoundField DataField="publication_year" HeaderText="PUBLISH YEAR" />
            <asp:BoundField DataField="retail_price" HeaderText="PRICE" />
        </Columns>
        <HeaderStyle BackColor="#66CCFF" />
    </asp:GridView>
    </form>
</body>
</html>

In above code select all checkbox added on header of the itemtemplate gridview. Now run the page for output


DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top