February 27, 2015 06:16 by
Peter
In this post I will tell you about select or deselect all checkbox inside gridview using jquery in ASP.NET 5. First, create a new project and then write the following code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewSelectAll.aspx.cs"
Inherits="GridViewSelectAll" %>
<!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></title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#<%=gvProducts.ClientID%> input[id$='chkAll']:checkbox").click(function () {
$("#<%=gvProducts.ClientID%> input[id*='chkSelected']:checkbox").attr('checked', $(this).is(':checked'));
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gvProducts" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input type="checkbox" id="chkAll" runat="server" value='<%#Eval("ProductID") %>' />
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" id="chkSelected" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" />
<asp:BoundField DataField="Description" HeaderText="Description" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GridViewSelectAll : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
gvProducts.DataSource = ProductDAL.GetProducts();
gvProducts.DataBind();
}
}
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public string Description { get; set; }
}
public class ProductDAL
{
public static List<Product> GetProducts()
{
return new List<Product>()
{
new Product() {ProductID = 1, ProductName = "Product01", Description = "Example01"},
new Product() {ProductID = 2, ProductName = "Product02", Description = "Example02"},
new Product() {ProductID = 3, ProductName = "Product03", Description = "Example03"},
new Product() {ProductID = 4, ProductName = "Product04", Description = "Example04"},
new Product() {ProductID = 5, ProductName = "Product05", Description = "Example05"},
};
}
}
HostForLIFE.eu ASP.NET 5 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.