GridView 实现服务器端和客户端全选的两种方法
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script language="javascript" type="text/javascript">![](https://www.cnblogs.com/Images/dot.gif)
function CheckAll(oCheckbox)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var GridView2 = document.getElementById("<%=GridView2.ClientID %>");
for(i = 1;i < GridView2.rows.length; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GridView2.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = oCheckbox.checked;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
</script>
![](/Images/OutliningIndicators/None.gif)
<table style="width:800px;font-size:12px;">
<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>
ICollection CreateDataSource()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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)));
![](/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < 8; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
}
![](/Images/OutliningIndicators/None.gif)
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (!IsPostBack)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
GridView2.DataSource = GridView1.DataSource = CreateDataSource();
GridView2.DataBind();
GridView1.DataBind();
}
}
![](/Images/OutliningIndicators/None.gif)
protected void Button1_Click(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Ret1.Text = "";
foreach (GridViewRow gvr in GridView1.Rows)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
if (ch.Checked)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Ret1.Text += "<li>GridView1 您选择的是(键值):" + GridView1.DataKeys[gvr.DataItemIndex].Value.ToString();
}
}
}
![](/Images/OutliningIndicators/None.gif)
protected void Button2_Click(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Ret2.Text = "";
foreach (GridViewRow gvr in GridView2.Rows)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
if (ch.Checked)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Ret2.Text += "<li>GridView2 您选择的是(键值):" + GridView2.DataKeys[gvr.DataItemIndex].Value.ToString();
}
}
}
![](/Images/OutliningIndicators/None.gif)
protected void CheckAll(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
CheckBox cbx = (CheckBox)sender;
foreach (GridViewRow gvr in GridView1.Rows)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
ch.Checked = cbx.Checked;
}
}