前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="meno.aspx.cs" Inherits="gridviewselectAll.meno" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function chage(sender) {
var table = document.getElementById("GridView1");
for (var i = 1; i < table.rows.length; i++) {
table.rows[i].cells[0].getElementsByTagName("input")[0].checked = sender.checked;
//获取当前数据行中的第几列是checked然后让它等于上面checked事件
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
DeleteCommand="DELETE FROM [student] WHERE [sid] = @sid"
InsertCommand="INSERT INTO [student] ([sname], [sex], [age]) VALUES (@sname, @sex, @age)"
SelectCommand="SELECT [sid], [sname], [sex], [age] FROM [student]"
UpdateCommand="UPDATE [student] SET [sname] = @sname, [sex] = @sex, [age] = @age WHERE [sid] = @sid">
<DeleteParameters>
<asp:Parameter Name="sid" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Int32" />
<asp:Parameter Name="sid" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="sid" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True"
oncheckedchanged="CheckBox2_CheckedChanged1" />
<br />
<input id="Checkbox3" type="checkbox" οnclick="chage(this)"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sid" HeaderText="sid" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" />
<asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace gridviewselectAll
{
public partial class meno : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CheckBox2_CheckedChanged1(object sender, EventArgs e)
{
CheckBox ckb = sender as CheckBox;
foreach (GridViewRow row in this.GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
(row.Cells[4].FindControl("CheckBox1") as CheckBox).Checked = ckb.Checked;
}
}
}
}
}