Gridview Select Multiple Checkbox in ASP.NET
HTML Markup : Add Gridview to WebPage
<asp:GridView ID="grdview1" runat="server" AutoGenerateColumns="False"
onrowdatabound="grdview1_RowDataBound" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
ForeColor="Black" GridLines="Vertical">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chk_parent_grdview" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk_child_grdview" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book ID">
<ItemTemplate>
<asp:Label ID="lbl_bookid" runat="server" Text='<%# Evalundefined"Book_Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Name">
<ItemTemplate>
<asp:Label ID="lbl_bookname" runat="server" Text='<%# Evalundefined"Book_name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Author">
<ItemTemplate>
<asp:Label ID="lbl_bookauthor" runat="server" Text='<%# Evalundefined"Book_author") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Publisher Name">
<ItemTemplate>
<asp:Label ID="lbl_bookpubname" runat="server" Text='<%# Evalundefined"Book_Publisher_name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
Add Javascript to WebPage
<script type="text/javascript">
function SelectAll(id) {
var grid = document.getElementById("<%= grdview1.ClientID %>");
var cell;
if (grid.rows.length > 0) {
for (i = 1; i < grid.rows.length; i++) {
cell = grid.rows[i].cells[0];
for (j = 0; j < cell.childNodes.length; j++) {
if (cell.childNodes[j].type == "checkbox") {
cell.childNodes[j].checked = document.getElementById(id).checked;
}
}
}
}
}
</script>
C# Coding
C# Coding : Namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
C# Coding : Page Load
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ms"].ConnectionString);
SqlDataAdapter da;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
FillGrdView();
}
C# Coding : Gridview
private void FillGrdView()
{
da = new SqlDataAdapter("Select * from Book", con);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
grdview1.DataSource = ds;
grdview1.DataBind();
}
}
protected void grdview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//header select all function
if (e.Row.RowType == DataControlRowType.Header)
{
((CheckBox)e.Row.FindControl("chk_parent_grdview")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("chk_parent_grdview")).ClientID + "')");
}
}
}
0 Comments