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)
...{
ViewState["SortOrder"] = "firstname";
ViewState["OrderDire"] = "asc";
GridViewDataBind();
}
}
private void GridViewDataBind()
...{
string connStr = ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter da = new SqlDataAdapter("select firstname, lastname, city, birthdate from employees", conn);
conn.Open();
DataSet ds = new DataSet();
try
...{
da.Fill(ds, "testTable");
DataView view = ds.Tables["testTable"].DefaultView;
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
view.Sort = sort;
//dgCheckBox.DataSource = ds.Tables["testTable"].DefaultView;
dgCheckBox.DataSource = view;
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_Sorting(object sender, GridViewSortEventArgs e)
...{
string vortExp = e.SortExpression;
if (ViewState["SortOrder"].ToString() == vortExp)
...{
if ((string)ViewState["OrderDire"] == "desc")
...{
ViewState["OrderDire"] = "asc";
}
else
...{
ViewState["OrderDire"] = "asc";
}
}
else
...{
ViewState["SortOrder"] = e.SortExpression;
}
GridViewDataBind();
}
protected void dgCheckBox_PageIndexChanging(object sender, GridViewPageEventArgs e)
...{
dgCheckBox.PageIndex = e.NewPageIndex;
GridViewDataBind();
}
}
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)
...{
ViewState["SortOrder"] = "firstname";
ViewState["OrderDire"] = "asc";
GridViewDataBind();
}
}
private void GridViewDataBind()
...{
string connStr = ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter da = new SqlDataAdapter("select firstname, lastname, city, birthdate from employees", conn);
conn.Open();
DataSet ds = new DataSet();
try
...{
da.Fill(ds, "testTable");
DataView view = ds.Tables["testTable"].DefaultView;
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
view.Sort = sort;
//dgCheckBox.DataSource = ds.Tables["testTable"].DefaultView;
dgCheckBox.DataSource = view;
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_Sorting(object sender, GridViewSortEventArgs e)
...{
string vortExp = e.SortExpression;
if (ViewState["SortOrder"].ToString() == vortExp)
...{
if ((string)ViewState["OrderDire"] == "desc")
...{
ViewState["OrderDire"] = "asc";
}
else
...{
ViewState["OrderDire"] = "asc";
}
}
else
...{
ViewState["SortOrder"] = e.SortExpression;
}
GridViewDataBind();
}
protected void dgCheckBox_PageIndexChanging(object sender, GridViewPageEventArgs e)
...{
dgCheckBox.PageIndex = e.NewPageIndex;
GridViewDataBind();
}
}
GridViewCheckBox.aspx代码如下:
<
form id
=
"
form1
"
runat
=
"
server
"
>
< div >
< table style = " width: 566px " >
< tr >
< td style = " width: 112px " >
< asp:GridView ID = " dgCheckBox " runat = " server " GridLines = " vertical " AutoGenerateColumns = " false " PagerSettings - Mode = " numeric " Width = " 446px "
AllowSorting = " true " AllowPaging = " true " PageSize = " 5 " OnSorting = " dgCheckBox_Sorting " OnPageIndexChanging = " dgCheckBox_PageIndexChanging " >
< 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 " SortExpression = " lastname " />
< asp:BoundField DataField = " firstname " HeaderText = " firstname " SortExpression = " firstname " />
< asp:BoundField DataField = " BirthDate " HeaderText = " BirthDate " SortExpression = " BirthDate " DataFormatString = " {0:D} " />
< asp:TemplateField >
< ItemTemplate >
< asp:Label ID = " lblCity " runat = " server " Text = ' <%#Eval("city") %> ' ></ asp:Label >
</ ItemTemplate >
</ asp:TemplateField >
</ Columns >
< PagerStyle HorizontalAlign = " Right " 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 >
</ div >
</ form >
< div >
< table style = " width: 566px " >
< tr >
< td style = " width: 112px " >
< asp:GridView ID = " dgCheckBox " runat = " server " GridLines = " vertical " AutoGenerateColumns = " false " PagerSettings - Mode = " numeric " Width = " 446px "
AllowSorting = " true " AllowPaging = " true " PageSize = " 5 " OnSorting = " dgCheckBox_Sorting " OnPageIndexChanging = " dgCheckBox_PageIndexChanging " >
< 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 " SortExpression = " lastname " />
< asp:BoundField DataField = " firstname " HeaderText = " firstname " SortExpression = " firstname " />
< asp:BoundField DataField = " BirthDate " HeaderText = " BirthDate " SortExpression = " BirthDate " DataFormatString = " {0:D} " />
< asp:TemplateField >
< ItemTemplate >
< asp:Label ID = " lblCity " runat = " server " Text = ' <%#Eval("city") %> ' ></ asp:Label >
</ ItemTemplate >
</ asp:TemplateField >
</ Columns >
< PagerStyle HorizontalAlign = " Right " 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 >
</ div >
</ form >
GridViewCheckBox.aspx.cs代码如下: