Friday, 4 October 2013

How to select all the Checkboxes in Gridview by clicking on Checkbox in HeaderText in ASP.NET using Javascript

Hi friends, in this article I will explain about How to select all the Checkboxes in Gridview by clicking on Checkbox in HeaderText in ASP.NET using Javascript 
In ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Select all checkboxes in GridView in ASP.NET </title>
    <script type ="text/javascript" language ="javascript" >
        function HeaderSelect(selval) {
            if (selval == true) {
                CheckUncheckall(true);
            }
            else {
                CheckUncheckall(false);
            }
        }
        function CheckUncheckall(selval) {
            var parentcontrol = document.getElementById('<%= GridView1.ClientID %>');
            var inputctrls = parentcontrol.getElementsByTagName("input");

            for (var i = 0; i < inputctrls.length; i++) {
                if (inputctrls[i].type == 'checkbox') {
                    inputctrls[i].checked = selval;
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
          <HeaderStyle BackColor="#E9F3F8" Height="40px" />
          <RowStyle BorderColor="#DDDDDD" HorizontalAlign="Left" VerticalAlign="Middle" Height="40px" />
          <AlternatingRowStyle Height="40px" BackColor="#F7FAFC" />
          <Columns >
          <asp:TemplateField>
            <HeaderStyle HorizontalAlign="Center" Width="45" BorderColor="#DDDDDD"></HeaderStyle>
              <HeaderTemplate>
                  <input name="Cbx_QRHeader" onclick="HeaderSelect(this.checked);" type="checkbox">
              </HeaderTemplate>
             <ItemTemplate>
                  <input type="checkbox" name="Cbx_Cid" value='<%#DataBinder.Eval(Container.DataItem, "userid")%>%>'>
             </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" BorderColor="#DDDDDD" />
            <HeaderStyle HorizontalAlign="Center" BorderColor="#DDDDDD" Width="30px" />
           </asp:TemplateField>
          </Columns>
        </asp:GridView>
    </div>       
    </form>
</body>
</html>


In C#.NET:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class AutoComplete : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("select * from user_data", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }
}

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.