我们用代码来看这个问题:
1)、
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function checkall() { //此函数是为全选按钮设计的函数。
var input = document.getElementsByTagName('input'); //设置变量来接收所有的input标签
for (var i = 0; i < input.length; i++) { //遍历input 标签
if (input[i].type == 'checkbox') { //判断input标签的类型是否为checkbox类型
input[i].checked = document.getElementById('Checkall').checked; //input标签为checkbox类型,设置它的状态与全选按钮的状态为一致。全选按钮为选中则全部checkbox选中,反之则都不选中。
}
if (document.getElementById('Checkall').checked == true) { //此判断判断当全选按钮被选中够则在全选按钮后显示“取消全选”,全选按钮没被选中则在全选按钮后显示“全选”。
document.getElementById('spaninfo').innerHTML = "取消全选";
} else {
document.getElementById('spaninfo').innerHTML = "全选";
}
}
}
function notcheck() { //此函数是为反选实际的函数,反选就是你选中几个,当按反选的时候,选中的取消,没选中的都选中。
var input = document.getElementsByTagName('input');
for (var i = 0; i < input.length; i++) { //遍历input标签
if (input[i].type == 'checkbox') {
if (input[i].checked == true) { //此判断就是检查判断所有的checkbox,若其中有被选中的变为不选中,没选中的为选中。
input[i].checked = false;
} else {
input[i].checked = true;
}
document.getElementById('Checkall').checked = false;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table><tr><td><input id="Checkall" type="checkbox" οnclick="checkall()"/></td><td style="width:65px;"><span id="spaninfo">全选</span></td><td> //此为全选按钮
<input id="Button1" type="button" value="添加" οnclick="addwebtable()" /></td>
<td><input
id="Button4" type="button" value="反选" οnclick="notcheck()"/></td> //此为反选按钮
<td><asp:Button
ID="Button5" runat="server" Text="删除" OnClientClick="check()" οnclick="Button5_Click" /></td> //此为全选多选删除按钮,下面的后台代码就是该按钮的。
<td> </tr></table>
<--这段代码可以不看--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------->
<asp:GridView ID="GridView1" runat="server"
Width="798px" CellPadding="4" ForeColor="#333333"
AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="用户名" >
<ItemStyle Width="120px" />
</asp:BoundField>
<asp:BoundField DataField="RealName" HeaderText="姓名" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="家庭地址" >
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:ImageField DataImageUrlField="imageurl" HeaderText="照片">
<ControlStyle Height="30px" Width="30px" />
<ItemStyle Width="120px" />
</asp:ImageField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:Button ID="Button2" runat="server" Text="修改"
CommandName="Revise" CommandArgument='<%#Eval("UserName")%>' />
<asp:Button ID="Button3" runat="server" Text="删除"
CommandName="butDelete" CommandArgument='<%#Eval("UserName") %>' />
</ItemTemplate>
<ItemStyle Width="120px" />
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<PagerTemplate>
</PagerTemplate>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------->
</form>
</body>
</html>
2)、删除按钮的后台代码。
protected void Button5_Click(object sender, EventArgs e)
{
int checknumber = 0;
string deletestr = "";
for (int i = 0; i < GridView1.Rows.Count; i++) //遍历GridView的所有行
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow) //判断GridView所有行的数据类型,找出其中的数据行。
{
CheckBox check = GridView1.Rows[i].FindControl("CheckBox1") as CheckBox; //将所有数据行中checkbox控件创建实例转化为checkbox类型。
if (check.Checked == true)
{
deletestr += "'" + GridView1.Rows[i].Cells[1].Text + "',"; //获取所有数据行里checkbox被选中的行的第二列(也就是我例子中用户名的值)。并且用字符串将所有选中的用户名拼接起来。
checknumber++;
}
}
}
if (checknumber > 0)
{
deletestr = deletestr.Trim(','); //去掉拼接是首位多出来的逗号。
string connection1 = ConfigurationManager.ConnectionStrings["serverstr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connection1))
{
conn.Open();
using (SqlCommand command = conn.CreateCommand())
{
command.CommandText = "delete from T_student1 where UserName in(" + deletestr + ")"; //将拼接的字符串放到Sql语句中。完成多选删除
if (command.ExecuteNonQuery() > 0)
{
connection();
pageindexinfo();
//sum();
}
}
}
}
}