页面要添加的JS
<script type="text/javascript">
function selectAll(obj)
{
var theTable = obj.parentElement.parentElement.parentElement;
var i;
var j = obj.parentElement.cellIndex;
for(i=0;i<theTable.rows.length;i++)
{
var objCheckBox = theTable.rows[i].cells[j].firstChild;
if(objCheckBox.checked!=null)objCheckBox.checked = obj.checked;
}
}
</script>
GridView控件添加模板
<asp:TemplateField HeaderText="选择">
<ItemStyle Width="100px" />
<HeaderTemplate>
<input id="CheckAll" onclick="selectAll(this);" type="checkbox" />本页全选
<ItemStyle Width="100px" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkOk" runat="server" />
<ItemStyle Width="100px" />
</ItemTemplate>
</asp:TemplateField>
aspx文件代码
for (int i = 0; i < this.gridview.Rows.Count; i++)
{
bool isChecked = ((CheckBox)gridview.Rows[i].FindControl("chkOk")).Checked;
if (isChecked)
{
选中项要执行的操作
}
}