Checkbox column in Gridview(Javascript in asp.net)
**************************************Javascripts**********************************************************
//CheckBox全选
function CA()
{
var frm = document.Form1;
for (var i = 0; i < frm.elements.length; i++)
{
var e = frm.elements[i];
if ((e.name != 'allbox') && (e.type == 'checkbox'))
{
e.checked = frm.allbox.checked;
if (frm.allbox.checked)
{
hL(e);
} //endif
else
{
dL(e);
} //endelse
} //endif
} //endfor
}
//CheckBox选择项
function CCA(CB)
{
var frm = document.Form1;
if (CB.checked)
hL(CB);
else
dL(CB);
var TB = TO = 0;
for (var i = 0; i < frm.elements.length; i++)
{
var e = frm.elements[i];
if ((e.name != 'allbox') && (e.type == 'checkbox'))
{
TB++;
if (e.checked)
TO++;
}
}
frm.allbox.checked = (TO == TB) ? true : false;
}
function hL(E)
{
while (E.tagName != "TR")
{ E = E.parentElement; }
E.className = "H";
}
function dL(E)
{
while (E.tagName != "TR")
{ E = E.parentElement; }
E.className = "";
}
*****************************************************************************************
****************************************.aspx page*****************************************
<%@ Page language="c#" Codebehind="CheckBoxOnDataGrid.aspx.cs" AutoEventWireup="false" Inherits="Text_WebApp.CheckBoxOnDataGrid" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>CheckBoxOnDataGrid</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="JavaScript" src="CheckBox.js"></script>
<LINK href="style.css" rel="stylesheet" type="text/css">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" name="Form1">
<FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face=宋体></FONT>
<br>
<ASP:DataGrid id="MyDataGrid" runat="server" Width="696px" BackColor="white" BorderColor="black"
CellPadding="3" CellSpacing="0" Font-Size="9pt"
AutoGenerateColumns="False" HeaderStyle-BackColor="darkred"
HeaderStyle-ForeColor="white" Height="160px" AllowPaging="True"
onpageindexchanged="MyDataGrid_PageIndexChanged">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<input type=checkbox name="allbox" onclick="CA();">
<font face="Webdings" color="white" size="4">a</font>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="DeleteThis" onclick="javascript:CCA(this);" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
ID
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="StoreID" Text='<%# DataBinder.Eval (Container.DataItem, "ID") %>' runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="Store" Datafield="Store" runat="server" />
</Columns>
<HeaderStyle BackColor="DarkRed" ForeColor="White"></HeaderStyle>
</ASP:DataGrid>
<br>
<asp:Button Text="Delete Items" ID="Confirm" runat="server" />
<span id="OutputMsg" EnableViewState="false" runat="server" />
</form>
</body>
</HTML>
*****************************************************************************************
****************************************.aspx.cs page****************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace Text_WebApp
{
/**//// <summary>
/// CheckBoxOnDataGrid 的摘要说明。
/// </summary>
public partial class CheckBoxOnDataGrid : System.Web.UI.Page
{
private SqlConnection objConnect;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
objConnect = new SqlConnection("server=(local);database=OrbitPipelinePIMSDemo;Integrated Security=SSPI;");
if (!IsPostBack)
{
BindData();
}
}
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Confirm.Click += new System.EventHandler(this.Confirm_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
public void BindData()
{
String sqlQuery = "Select C_OID As Id, C_UID As Store from PIMS_C_LOCATION";
DataSet ds=new DataSet();
SqlDataAdapter Sda=new SqlDataAdapter(sqlQuery,objConnect);
Sda.Fill(ds,"Tb");
MyDataGrid.DataSource =ds.Tables["Tb"];
MyDataGrid.DataBind();
objConnect.Close();
objConnect = null;
}
private void Confirm_Click(object sender, System.EventArgs e)
{
string dgIDs = "";
bool BxsChkd = false;
foreach (DataGridItem i in MyDataGrid.Items)
{
CheckBox deleteChkBxItem = (CheckBox) i.FindControl ("DeleteThis");
if (deleteChkBxItem.Checked)
{
BxsChkd = true;
dgIDs += ((Label) i.FindControl ("StoreID")).Text.ToString() + ",";
}
}
string deleteSQL = "DELETE from Stores WHERE stor_id IN (" + dgIDs.Substring (0, dgIDs.LastIndexOf (",")) + ")";
if (BxsChkd == true)
{
try
{
//SqlHelper.ExecuteNonQuery (objConnect, CommandType.Text, deleteSQL);
OutputMsg.InnerHtml+="删除"+dgIDs.ToString().Trim()+"成功";
OutputMsg.Style["color"] = "#3366cc";
}
catch (SqlException err)
{
OutputMsg.InnerHtml += err.Message.ToString();
OutputMsg.Style["color"] = "#6699cc";
}
BindData();
}
}
protected void MyDataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
BindData();
}
}
}
****************************************************************************************************
******************************************CSS*******************************************************
.{
font-size : 9pt;
}
.H{
font-size : 9pt;
cursor:hand;
color:#3366ff;
background : #FFF3F3;
}
//CheckBox全选
function CA()
{
var frm = document.Form1;
for (var i = 0; i < frm.elements.length; i++)
{
var e = frm.elements[i];
if ((e.name != 'allbox') && (e.type == 'checkbox'))
{
e.checked = frm.allbox.checked;
if (frm.allbox.checked)
{
hL(e);
} //endif
else
{
dL(e);
} //endelse
} //endif
} //endfor
}
//CheckBox选择项
function CCA(CB)
{
var frm = document.Form1;
if (CB.checked)
hL(CB);
else
dL(CB);
var TB = TO = 0;
for (var i = 0; i < frm.elements.length; i++)
{
var e = frm.elements[i];
if ((e.name != 'allbox') && (e.type == 'checkbox'))
{
TB++;
if (e.checked)
TO++;
}
}
frm.allbox.checked = (TO == TB) ? true : false;
}
function hL(E)
{
while (E.tagName != "TR")
{ E = E.parentElement; }
E.className = "H";
}
function dL(E)
{
while (E.tagName != "TR")
{ E = E.parentElement; }
E.className = "";
}
*****************************************************************************************
****************************************.aspx page*****************************************
<%@ Page language="c#" Codebehind="CheckBoxOnDataGrid.aspx.cs" AutoEventWireup="false" Inherits="Text_WebApp.CheckBoxOnDataGrid" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>CheckBoxOnDataGrid</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="JavaScript" src="CheckBox.js"></script>
<LINK href="style.css" rel="stylesheet" type="text/css">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" name="Form1">
<FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face=宋体></FONT>
<br>
<ASP:DataGrid id="MyDataGrid" runat="server" Width="696px" BackColor="white" BorderColor="black"
CellPadding="3" CellSpacing="0" Font-Size="9pt"
AutoGenerateColumns="False" HeaderStyle-BackColor="darkred"
HeaderStyle-ForeColor="white" Height="160px" AllowPaging="True"
onpageindexchanged="MyDataGrid_PageIndexChanged">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<input type=checkbox name="allbox" onclick="CA();">
<font face="Webdings" color="white" size="4">a</font>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="DeleteThis" onclick="javascript:CCA(this);" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
ID
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="StoreID" Text='<%# DataBinder.Eval (Container.DataItem, "ID") %>' runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="Store" Datafield="Store" runat="server" />
</Columns>
<HeaderStyle BackColor="DarkRed" ForeColor="White"></HeaderStyle>
</ASP:DataGrid>
<br>
<asp:Button Text="Delete Items" ID="Confirm" runat="server" />
<span id="OutputMsg" EnableViewState="false" runat="server" />
</form>
</body>
</HTML>
*****************************************************************************************
****************************************.aspx.cs page****************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace Text_WebApp
{
/**//// <summary>
/// CheckBoxOnDataGrid 的摘要说明。
/// </summary>
public partial class CheckBoxOnDataGrid : System.Web.UI.Page
{
private SqlConnection objConnect;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
objConnect = new SqlConnection("server=(local);database=OrbitPipelinePIMSDemo;Integrated Security=SSPI;");
if (!IsPostBack)
{
BindData();
}
}
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Confirm.Click += new System.EventHandler(this.Confirm_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
public void BindData()
{
String sqlQuery = "Select C_OID As Id, C_UID As Store from PIMS_C_LOCATION";
DataSet ds=new DataSet();
SqlDataAdapter Sda=new SqlDataAdapter(sqlQuery,objConnect);
Sda.Fill(ds,"Tb");
MyDataGrid.DataSource =ds.Tables["Tb"];
MyDataGrid.DataBind();
objConnect.Close();
objConnect = null;
}
private void Confirm_Click(object sender, System.EventArgs e)
{
string dgIDs = "";
bool BxsChkd = false;
foreach (DataGridItem i in MyDataGrid.Items)
{
CheckBox deleteChkBxItem = (CheckBox) i.FindControl ("DeleteThis");
if (deleteChkBxItem.Checked)
{
BxsChkd = true;
dgIDs += ((Label) i.FindControl ("StoreID")).Text.ToString() + ",";
}
}
string deleteSQL = "DELETE from Stores WHERE stor_id IN (" + dgIDs.Substring (0, dgIDs.LastIndexOf (",")) + ")";
if (BxsChkd == true)
{
try
{
//SqlHelper.ExecuteNonQuery (objConnect, CommandType.Text, deleteSQL);
OutputMsg.InnerHtml+="删除"+dgIDs.ToString().Trim()+"成功";
OutputMsg.Style["color"] = "#3366cc";
}
catch (SqlException err)
{
OutputMsg.InnerHtml += err.Message.ToString();
OutputMsg.Style["color"] = "#6699cc";
}
BindData();
}
}
protected void MyDataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
BindData();
}
}
}
****************************************************************************************************
******************************************CSS*******************************************************
.{
font-size : 9pt;
}
.H{
font-size : 9pt;
cursor:hand;
color:#3366ff;
background : #FFF3F3;
}