一;aspx 页面
① 在GridView 的<Columns> 里面添加模板列代码如下:
<asp:TemplateField ItemStyle-Width="30px">
<HeaderTemplate>
<asp:CheckBox ID="CheckAll" runat="server" onclick="CheckAll(this)"/>
<asp:Label ID="Label1" runat="server" Text="全选"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CbClean" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
② 起作用的 js 代码
<script type="text/javascript">
function CheckAll(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>
③ CS后台代码得到选中行
protected void BtClean_Click(object sender, EventArgs e)
{
int j = 0;
for (int i = 0; i <= gvCleanList.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)gvCleanList.Rows[i].FindControl("CbClean");
if (cbox.Checked == true)
{
HfCleanCarId.Value += gvCleanList.DataKeys[i].Value + "@";
j++;
}
}
if (j < 1)
{
Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('请先选择车Ì辆');</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('”+ HfCleanCarId.Value +”');</script>");
}
}