<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Design.aspx.cs" Inherits="WebApplication1.Design" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function checkAll(GridObj) {
var GridView = GridObj.parentNode.parentNode.parentNode.parentNode;
var inputList = GridView.getElementsByTagName("input");
for (var i = 0; i < inputList.length; i++) {
if (inputList[i].type == "checkbox" && GridObj != inputList[i]) {
if (GridObj.checked) {
inputList[i].checked = true;
}
else {
inputList[i].checked = false;
}
}
}
}
function CheckChild(GridObj) {
var GridView = GridObj.parentNode.parentNode.parentNode.parentNode;
var inputList = GridView.getElementsByTagName("input");
var totalRow = inputList.length - 1;
var totalChk = 0;
for (var i = 0; i < inputList.length; i++) {
var headerCheckBox = inputList[0];
if (inputList[i].type == "checkbox" && inputList[i] != headerCheckBox) {
if (inputList[i].checked) {
totalChk++;
}
}
}
if (totalRow == totalChk)
headerCheckBox.checked = true;
else
headerCheckBox.checked = false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" GridLines="Both" CellPadding="4" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="checkChild" runat="server" onclick="CheckChild(this)" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</div>
</form>
</body>
</html>
Step 2: - Design.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
DataRow dr = null;
for (int i = 0; i < 5; i++)
{
dr = dt.NewRow();
dr["Column1"] = i;
dr["Column2"] = i;
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
Output
No comments:
Post a Comment