Articles → JQUERY → Check And Uncheck All Checkboxes Using Jquery And Asp.Net
Check And Uncheck All Checkboxes Using Jquery And Asp.Net
Software Requirement
- Visual studio 2005 (or above) is installed on your machine.
- Latest jQuery file i.e. jquery-latest.js (from the path http://code.jquery.com/jquery-latest.js) is downloaded on your system.
Prerequisite Knowledge
- How to create a website (web project) in asp.net?
- What are asp.net server controls (specially checkbox and gridview)?
- How to bind data in gridview?
- What are template fields in gridview?
- What is the purpose of ready method in JQuery?
- How to add events to HTML elements in jQuery?
Creation Of A Project
- Check_All_Demo.aspx
- Content.xml
Click to Enlarge
Adding Content In Xml File
<root>
<book content="Content1"></book>
<book content="Content2"></book>
</root>
Add Gridview In Aspx Page
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Content">
<ItemTemplate>
<asp:Label ID="lblContent" runat="server" Text='<% #Eval("Content") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Adding ‘Delete All’ Checkbox On The Page
<asp:CheckBox runat="server" ID="chkDeleteAll" Text="Delete All" />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Content">
<ItemTemplate>
<asp:Label ID="lblContent" runat="server" Text='<% #Eval("Content") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Bind Grid From Xml
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Content.xml"));
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
Adding Jquery Code
<script type="text/javascript">
$(document).ready(function() {
$("#<%=chkDeleteAll.ClientID %>").click(function() {
$("#<%=GridView1.ClientID %> tr td").find("input:checkbox").each(function() {
if($(this).attr("checked") == undefined) {
$(this).attr("checked", 'checked');
} else if($(this).attr("checked") == 'checked') {
$(this).removeAttr("checked");
}
});
});
});
</script>
Output
Click to Enlarge
Complete Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Check_All_Demo.aspx.cs" Inherits="Check_All_Demo" %>
<!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="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#<%=chkDeleteAll.ClientID %>").click(function() {
$("#<%=GridView1.ClientID %> tr td").find("input:checkbox").each(function() {
if($(this).attr("checked") == undefined) {
$(this).attr("checked", 'checked');
} else if($(this).attr("checked") == 'checked') {
$(this).removeAttr("checked");
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox runat="server" ID="chkDeleteAll" Text="Delete All" />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" /> </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Content">
<ItemTemplate>
<asp:Label ID="lblContent" runat="server" Text='<% #Eval("Content") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
public partial class Check_All_Demo: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Content.xml"));
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
}
<root>
<book content="Content1"></book>
<book content="Content2"></book>
</root>