代码很简单这里就不累述了看代码如下
C#
<%@ Page Language=C# AutoEventWireup=true %>
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN
transitionaldtd>
<script runat=server>
// 计算数据完全可以从数据看取得
ICollection CreateDataSource()
{
SystemDataDataTable dt = new SystemDataDataTable();
SystemDataDataRow dr;
dtColumnsAdd(new SystemDataDataColumn(序号 typeof(SystemString)));
dtColumnsAdd(new SystemDataDataColumn(学生姓名 typeof(SystemString)));
dtColumnsAdd(new SystemDataDataColumn(语文 typeof(SystemDecimal)));
dtColumnsAdd(new SystemDataDataColumn(数学 typeof(SystemDecimal)));
dtColumnsAdd(new SystemDataDataColumn(英语 typeof(SystemDecimal)));
dtColumnsAdd(new SystemDataDataColumn(计算机 typeof(SystemDecimal)));
for (int i = ; i < ; i++)
{
SystemRandom rd = new SystemRandom(EnvironmentTickCount * i); ;
dr = dtNewRow();
dr[] = iToString();
dr[] = 【孟子E章】 + iToString();
dr[] = SystemMathRound(rdNextDouble() * );
dr[] = SystemMathRound(rdNextDouble() * );
dr[] = SystemMathRound(rdNextDouble() * );
dr[] = SystemMathRound(rdNextDouble() * );
dtRowsAdd(dr);
}
SystemDataDataView dv = new SystemDataDataView(dt);
return dv;
}
protected void Page_Load(object sender EventArgs e)
{
if (!IsPostBack)
{
GridViewDataSource = GridViewDataSource = CreateDataSource();
GridViewDataBind();
GridViewDataBind();
}
}
protected void Button_Click(object sender EventArgs e)
{
RetText = ;
foreach (GridViewRow gvr in GridViewRows)
{
CheckBox ch = (CheckBox)gvrFindControl(ItemCheckBox);
if (chChecked)
{
RetText += <li>GridView 您选择的是(键值) + GridViewDataKeys[gvrDataItemIndex]ValueToString();
}
}
}
protected void Button_Click(object sender EventArgs e)
{
RetText = ;
foreach (GridViewRow gvr in GridViewRows)
{
CheckBox ch = (CheckBox)gvrFindControl(ItemCheckBox);
if (chChecked)
{
RetText += <li>GridView 您选择的是(键值) + GridViewDataKeys[gvrDataItemIndex]ValueToString();
}
}
}
protected void CheckAll(object sender EventArgs e)
{
CheckBox cbx = (CheckBox)sender;
foreach (GridViewRow gvr in GridViewRows)
{
CheckBox ch = (CheckBox)gvrFindControl(ItemCheckBox);
chChecked = cbxChecked;
}
}
</script>
<html xmlns=>
<head>
<title>GridView 实现服务器端和客户端全选的两种方法</title>
<script type=text/javascript>
//<![CDATA[
function CheckAll(oCheckbox)
{
var GridView = documentgetElementById(<%=GridViewClientID %>);
for(i = ;i < GridViewrowslength; i++)
{
GridViewrows[i]cells[]getElementsByTagName(INPUT)[]checked = oCheckboxchecked;
}
}
//]]>
</script>
</head>
<body>
<form id=Form runat=server>
<table >
<tr valign=top>
<td>
<asp:GridView ID=GridView runat=server FontSize=px BackColor=#FFFFFF
GridLines=Both CellPadding= DataKeyNames=序号 AutoGenerateColumns=false>
<HeaderStyle BackColor=#EDEDED Height=px />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID=CheckBox 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=Button runat=server OnClick=Button_Click Text=得到选择的行值 />
</td>
<td align=right>
<asp:GridView ID=GridView runat=server FontSize=px BackColor=#FFFFFF
GridLines=Both CellPadding= DataKeyNames=序号 AutoGenerateColumns=false>
<HeaderStyle BackColor=#EDEDED Height=px />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id=Checkbox 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=Button runat=server OnClick=Button_Click Text=得到选择的行值 />
</td>
</tr>
<tr valign=top>
<td>
<asp:Literal ID=Ret runat=server></asp:Literal>
</td>
<td align=right>
<asp:Literal ID=Ret runat=server></asp:Literal>
</td>
</tr>
</table>
</form>
</body>
</html>