代碼很簡單,這裡就不累述了。看代碼如下:
C#
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHtml 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xHtml1-transitional.dtd">
<script runat="server">
// 計算數據,完全可以從數據看取得
ICollection CreateDataSource()
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("序號", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("學生姓名", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("語文", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("數學", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("英語", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("計算機", typeof(System.Decimal)));
for (int i = 0; i < 8; i++)
{
System.Random rd = new System.Random(Environment.TickCount * i); ;
dr = dt.NewRow();
dr[0] = i.ToString();
dr[1] = "【孟子E章】" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataVIEw(dt);
return dv;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView2.DataSource = GridVIEw1.DataSource = CreateDataSource();
GridVIEw2.DataBind();
GridVIEw1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Ret1.Text = "";
foreach (GridViewRow gvr in GridVIEw1.Rows)
{
CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
if (ch.Checked)
{
Ret1.Text += "<li>GridView1 您選擇的是(鍵值):" + GridVIEw1.DataKeys[gvr.DataItemIndex].Value.ToString();
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Ret2.Text = "";
foreach (GridViewRow gvr in GridVIEw2.Rows)
{
CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
if (ch.Checked)
{
Ret2.Text += "<li>GridView2 您選擇的是(鍵值):" + GridVIEw2.DataKeys[gvr.DataItemIndex].Value.ToString();
}
}
}
protected void CheckAll(object sender, EventArgs e)
{
CheckBox cbx = (CheckBox)sender;
foreach (GridViewRow gvr in GridVIEw1.Rows)
{
CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
ch.Checked = cbx.Checked;
}
}
</script>
<html XMLns="http://www.w3.org/1999/xHtml">
<head>
<title>GridVIEw 實現服務器端和客戶端全選的兩種方法</title>
<script type="text/Javascript">
//<![CDATA[
function CheckAll(oCheckbox)
{
var GridView2 = document.getElementById("<%=GridView2.ClIEntID %>");
for(i = 1;i < GridVIEw2.rows.length; i++)
{
GridVIEw2.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = oCheckbox.checked;
}
}
//]]>
</script>
</head>
<body>
<form id="Form1" runat="server">
<table >
<tr valign="top">
<td>
<ASP:GridView ID="GridVIEw1" runat="server" Font-Size="12px" BackColor="#FFFFFF"
GridLines="Both" CellPadding="4" DataKeyNames="序號" AutoGenerateColumns="false">
<HeaderStyle BackColor="#EDEDED" Height="26px" />
<Columns>
<ASP:TemplateFIEld>
<HeaderTemplate>
<ASP:CheckBox ID="CheckBox1" runat="server" Text="全選" AutoPostBack="true" OnCheckedChanged="CheckAll" />
</HeaderTemplate>
<ItemTemplate>
<ASP:CheckBox ID="ItemCheckBox" runat="server" />
</ItemTemplate>
</ASP:TemplateFIEld>
<ASP:BoundField DataFIEld="學生姓名" HeaderText="學生姓名" />
<ASP:BoundField DataFIEld="語文" HeaderText="語文" />
<ASP:BoundField DataFIEld="數學" HeaderText="數學" />
<ASP:BoundField DataFIEld="英語" HeaderText="英語" />
<ASP:BoundField DataFIEld="計算機" HeaderText="計算機" />
</Columns>
</ASP:GridVIEw>
<ASP:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="得到選擇的行值" />
</td>
<td align="right">
<ASP:GridView ID="GridVIEw2" runat="server" Font-Size="12px" BackColor="#FFFFFF"
GridLines="Both" CellPadding="4" DataKeyNames="序號" AutoGenerateColumns="false">
<HeaderStyle BackColor="#EDEDED" Height="26px" />
<Columns>
<ASP:TemplateFIEld>
<HeaderTemplate>
<input id="Checkbox2" type="checkbox" onclick="CheckAll(this)" runat="server" /><label>全選</label>
</HeaderTemplate>
<ItemTemplate>
<ASP:CheckBox ID="ItemCheckBox" runat="server" />
</ItemTemplate>
</ASP:TemplateFIEld>
<ASP:BoundField DataFIEld="學生姓名" HeaderText="學生姓名" />
<ASP:BoundField DataFIEld="語文" HeaderText="語文" />
<ASP:BoundField DataFIEld="數學" HeaderText="數學" />
<ASP:BoundField DataFIEld="英語" HeaderText="英語" />
<ASP:BoundField DataFIEld="計算機" HeaderText="計算機" />
</Columns>
</ASP:GridVIEw>
<ASP:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="得到選擇的行值" />
</td>
</tr>
<tr valign="top">
<td>
<asp:Literal ID="Ret1" runat="server"></ASP:Literal>
</td>
<td align="right">
<asp:Literal ID="Ret2" runat="server"></ASP:Literal>
</td>
</tr>
</table>
</form>
</body>
</Html>