在aspx页面添加如下js脚本
<
script type
=
"
text/javascript
"
>
// <![CDATA[
function CheckAll(oCheckbox)
{
var GridView2 = document.getElementById( " <%=GridView1.ClientID %> " );
for (i = 1 ;i < GridView1.rows.length; i ++ )
{
GridView1.rows[i].cells[ 0 ].getElementsByTagName( " INPUT " )[ 0 ].checked = oCheckbox.checked;
}
}
// ]]>
</ script >
// <![CDATA[
function CheckAll(oCheckbox)
{
var GridView2 = document.getElementById( " <%=GridView1.ClientID %> " );
for (i = 1 ;i < GridView1.rows.length; i ++ )
{
GridView1.rows[i].cells[ 0 ].getElementsByTagName( " INPUT " )[ 0 ].checked = oCheckbox.checked;
}
}
// ]]>
</ script >
gridview中的声明部分代码如下
<
asp:TemplateField HeaderText
=
"
选择
"
>
< HeaderTemplate >
< input id = " Checkbox2 " type = " checkbox " onclick = " CheckAll(this) " runat = " server " />< label > 全选 </ label >
</ HeaderTemplate >
< ItemTemplate >
< asp:CheckBox ID = " CheckBox1 " runat = " server " Text = " 选择 " />
</ ItemTemplate >
</ asp:TemplateField >
< HeaderTemplate >
< input id = " Checkbox2 " type = " checkbox " onclick = " CheckAll(this) " runat = " server " />< label > 全选 </ label >
</ HeaderTemplate >
< ItemTemplate >
< asp:CheckBox ID = " CheckBox1 " runat = " server " Text = " 选择 " />
</ ItemTemplate >
</ asp:TemplateField >
如下图:
http://download1.youkuaiyun.com/down3/20070524/24093134446.gif
在一个按钮中判断是否行被选择
for
(
int
i
=
0
; i
<
this
.GridView1.Rows.Count; i
++
)
{
CheckBox myCb = (CheckBox)( this .GridView1.Rows[i].FindControl( " CheckBox1 " ));
if (myCb.Checked)
{
Label lb_nj = (Label)( this .GridView1.Rows[i].Cells[ 1 ].Controls[ 1 ]); // 年级
Label lb_zybh = (Label)( this .GridView1.Rows[i].Cells[ 3 ].Controls[ 1 ]); // 专业编号
Label lb_zymc = (Label)( this .GridView1.Rows[i].Cells[ 2 ].Controls[ 1 ]); // 专业名称
Label lb_ccbh = (Label)( this .GridView1.Rows[i].Cells[ 5 ].Controls[ 1 ]); // 层次编号
Label lb_ccmc = (Label)( this .GridView1.Rows[i].Cells[ 4 ].Controls[ 1 ]); // 层次名称
// ……………………
}
{
CheckBox myCb = (CheckBox)( this .GridView1.Rows[i].FindControl( " CheckBox1 " ));
if (myCb.Checked)
{
Label lb_nj = (Label)( this .GridView1.Rows[i].Cells[ 1 ].Controls[ 1 ]); // 年级
Label lb_zybh = (Label)( this .GridView1.Rows[i].Cells[ 3 ].Controls[ 1 ]); // 专业编号
Label lb_zymc = (Label)( this .GridView1.Rows[i].Cells[ 2 ].Controls[ 1 ]); // 专业名称
Label lb_ccbh = (Label)( this .GridView1.Rows[i].Cells[ 5 ].Controls[ 1 ]); // 层次编号
Label lb_ccmc = (Label)( this .GridView1.Rows[i].Cells[ 4 ].Controls[ 1 ]); // 层次名称
// ……………………
}