using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class GridViewCheckBox : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
if (!IsPostBack)
GridViewDataBind();
}
private void GridViewDataBind()
...{
string connStr = ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter da = new SqlDataAdapter("select firstname, lastname, city from employees", conn);
conn.Open();
DataSet ds = new DataSet();
try
...{
da.Fill(ds, "testTable");
dgCheckBox.DataSource = ds.Tables["testTable"].DefaultView;
dgCheckBox.DataBind();
}
catch (Exception error)
...{
Response.Write(error.ToString());
}
finally
...{
conn.Close();
}
}
protected void cmdSelectAll_Click(object sender, EventArgs e)
...{
System.Web.UI.WebControls.CheckBox chkEport;
if (cmdSelectAll.Text == "全选")
...{
foreach (GridViewRow gridviewrow in dgCheckBox.Rows)
...{
chkEport = (CheckBox)gridviewrow.FindControl("chkEport");
chkEport.Checked = true;
}
cmdSelectAll.Text = "取消";
}
else
...{
foreach (GridViewRow gridviewrow in dgCheckBox.Rows)
...{
chkEport = (CheckBox)gridviewrow.FindControl("chkEport");
chkEport.Checked = false;
}
cmdSelectAll.Text = "全选";
}
}
protected void cmdFindSelected_Click(object sender, EventArgs e)
...{
System.Web.UI.WebControls.CheckBox chkEport;
string strID;
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("选中项的City字段值分别为:<hr color=red>");
foreach (GridViewRow gridviewrow in dgCheckBox.Rows)
...{
chkEport = (CheckBox)gridviewrow.FindControl("chkEport");
if (chkEport.Checked)
...{
strID = ((Label)(gridviewrow.FindControl("lblCity"))).Text;
strMsg.Append(strID + "<br><hr color=red>");
}
message.Text = strMsg.ToString();
}
}
protected void dgCheckBox_RowCreated(object sender, GridViewRowEventArgs e)
...{
if (e.Row.RowType == DataControlRowType.DataRow)
...{
TableCell myCell;
myCell = e.Row.Cells[4];
LinkButton btnDel = (LinkButton)myCell.Controls[0];
btnDel.Attributes.Add("onclick", "return confirm('您真的要删除此行吗?');");
btnDel.Text = "删除";
}
}
protected void dgCheckBox_RowDeleting(object sender, GridViewDeleteEventArgs e)
...{
Response.Write("删除");
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class GridViewCheckBox : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
if (!IsPostBack)
GridViewDataBind();
}
private void GridViewDataBind()
...{
string connStr = ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter da = new SqlDataAdapter("select firstname, lastname, city from employees", conn);
conn.Open();
DataSet ds = new DataSet();
try
...{
da.Fill(ds, "testTable");
dgCheckBox.DataSource = ds.Tables["testTable"].DefaultView;
dgCheckBox.DataBind();
}
catch (Exception error)
...{
Response.Write(error.ToString());
}
finally
...{
conn.Close();
}
}
protected void cmdSelectAll_Click(object sender, EventArgs e)
...{
System.Web.UI.WebControls.CheckBox chkEport;
if (cmdSelectAll.Text == "全选")
...{
foreach (GridViewRow gridviewrow in dgCheckBox.Rows)
...{
chkEport = (CheckBox)gridviewrow.FindControl("chkEport");
chkEport.Checked = true;
}
cmdSelectAll.Text = "取消";
}
else
...{
foreach (GridViewRow gridviewrow in dgCheckBox.Rows)
...{
chkEport = (CheckBox)gridviewrow.FindControl("chkEport");
chkEport.Checked = false;
}
cmdSelectAll.Text = "全选";
}
}
protected void cmdFindSelected_Click(object sender, EventArgs e)
...{
System.Web.UI.WebControls.CheckBox chkEport;
string strID;
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("选中项的City字段值分别为:<hr color=red>");
foreach (GridViewRow gridviewrow in dgCheckBox.Rows)
...{
chkEport = (CheckBox)gridviewrow.FindControl("chkEport");
if (chkEport.Checked)
...{
strID = ((Label)(gridviewrow.FindControl("lblCity"))).Text;
strMsg.Append(strID + "<br><hr color=red>");
}
message.Text = strMsg.ToString();
}
}
protected void dgCheckBox_RowCreated(object sender, GridViewRowEventArgs e)
...{
if (e.Row.RowType == DataControlRowType.DataRow)
...{
TableCell myCell;
myCell = e.Row.Cells[4];
LinkButton btnDel = (LinkButton)myCell.Controls[0];
btnDel.Attributes.Add("onclick", "return confirm('您真的要删除此行吗?');");
btnDel.Text = "删除";
}
}
protected void dgCheckBox_RowDeleting(object sender, GridViewDeleteEventArgs e)
...{
Response.Write("删除");
}
}
GridViewCheckBox.aspx如下:
<
table style
=
"
width: 566px
"
>
< tr >
< td style = " width: 112px " >
< asp:GridView ID = " dgCheckBox " runat = " server " GridLines = " vertical " AutoGenerateColumns = " false " PagerSettings - Mode = " numeric " Width = " 326px " OnRowCreated = " dgCheckBox_RowCreated " OnRowDeleting = " dgCheckBox_RowDeleting " >
< SelectedRowStyle Font - Bold = " True " ForeColor = " White " BackColor = " #008A8C " />
< AlternatingRowStyle BackColor = " AppWorkspace " />
< RowStyle ForeColor = " ActiveBorder " BackColor = " ActiveCaption " />
< HeaderStyle Font - Bold = " True " ForeColor = " ActiveCaptionText " BackColor = " AliceBlue " />
< FooterStyle ForeColor = " AppWorkspace " BackColor = " Aqua " />
< Columns >
< asp:TemplateField >
< ItemTemplate >
< asp:CheckBox ID = " chkEport " runat = " server " />
</ ItemTemplate >
< EditItemTemplate >
< asp:CheckBox ID = " chkEportON " runat = " server " />
</ EditItemTemplate >
</ asp:TemplateField >
< asp:BoundField DataField = " lastname " HeaderText = " lastname " />
< asp:BoundField DataField = " firstname " HeaderText = " firstname " />
< asp:TemplateField >
< ItemTemplate >
< asp:Label ID = " lblCity " runat = " server " Text = ' <%#Eval("city") %> ' ></ asp:Label >
</ ItemTemplate >
</ asp:TemplateField >
< asp:ButtonField Text = " 删除 " CommandName = " delete " />
</ Columns >
< PagerStyle HorizontalAlign = " Center " ForeColor = " Aquamarine " BackColor = " Azure " />
</ asp:GridView >
</ td >
</ tr >
< tr >
< td style = " width: 112px " >
< asp:Button ID = " cmdSelectAll " runat = " server " Text = " 全选 " OnClick = " cmdSelectAll_Click " />& nbsp; < asp:Button ID = " cmdFindSelected " runat = " server " Text = " 取得选择的项 " OnClick = " cmdFindSelected_Click " />
</ td >
</ tr >< tr >
< td >< asp:Label ID = " message " runat = " server " ></ asp:Label ></ td >
</ tr >
</ table >
< tr >
< td style = " width: 112px " >
< asp:GridView ID = " dgCheckBox " runat = " server " GridLines = " vertical " AutoGenerateColumns = " false " PagerSettings - Mode = " numeric " Width = " 326px " OnRowCreated = " dgCheckBox_RowCreated " OnRowDeleting = " dgCheckBox_RowDeleting " >
< SelectedRowStyle Font - Bold = " True " ForeColor = " White " BackColor = " #008A8C " />
< AlternatingRowStyle BackColor = " AppWorkspace " />
< RowStyle ForeColor = " ActiveBorder " BackColor = " ActiveCaption " />
< HeaderStyle Font - Bold = " True " ForeColor = " ActiveCaptionText " BackColor = " AliceBlue " />
< FooterStyle ForeColor = " AppWorkspace " BackColor = " Aqua " />
< Columns >
< asp:TemplateField >
< ItemTemplate >
< asp:CheckBox ID = " chkEport " runat = " server " />
</ ItemTemplate >
< EditItemTemplate >
< asp:CheckBox ID = " chkEportON " runat = " server " />
</ EditItemTemplate >
</ asp:TemplateField >
< asp:BoundField DataField = " lastname " HeaderText = " lastname " />
< asp:BoundField DataField = " firstname " HeaderText = " firstname " />
< asp:TemplateField >
< ItemTemplate >
< asp:Label ID = " lblCity " runat = " server " Text = ' <%#Eval("city") %> ' ></ asp:Label >
</ ItemTemplate >
</ asp:TemplateField >
< asp:ButtonField Text = " 删除 " CommandName = " delete " />
</ Columns >
< PagerStyle HorizontalAlign = " Center " ForeColor = " Aquamarine " BackColor = " Azure " />
</ asp:GridView >
</ td >
</ tr >
< tr >
< td style = " width: 112px " >
< asp:Button ID = " cmdSelectAll " runat = " server " Text = " 全选 " OnClick = " cmdSelectAll_Click " />& nbsp; < asp:Button ID = " cmdFindSelected " runat = " server " Text = " 取得选择的项 " OnClick = " cmdFindSelected_Click " />
</ td >
</ tr >< tr >
< td >< asp:Label ID = " message " runat = " server " ></ asp:Label ></ td >
</ tr >
</ table >
GridViewCheckBox.aspx.cs如下: