其他分享
首页 > 其他分享> > 全选,选中删除功能的实现(含母版页-精简)

全选,选中删除功能的实现(含母版页-精简)

作者:互联网

母版页

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Teacher.master.cs" Inherits="周五重点.Teacher" %>

<!DOCTYPE html>

<html>
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
        <p>欢迎登录:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>老师</p>
        <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </div>
    </form>
</body>
</html>
 

前台代码

 <%@ Page Title="" Language="C#" MasterPageFile="~/Teacher.Master" AutoEventWireup="true" CodeBehind="teacher.aspx.cs" Inherits="周五重点.teacher" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <style type="text/css">
        div{
            margin:0 auto;
            width:800px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div>
        <h1>学生信息</h1> 
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="UserID">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                       <asp:CheckBox ID="chkall" runat="server"  AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged"/>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkone" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="UserID" HeaderText="UserID" InsertVisible="False" ReadOnly="True" SortExpression="UserID" />
                <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
                <asp:BoundField DataField="UserPwd" HeaderText="UserPwd" SortExpression="UserPwd" />
                <asp:BoundField DataField="UserTel" HeaderText="UserTel" SortExpression="UserTel" />
                <asp:BoundField  DataField="UserBirth" HeaderText="UserBirth" SortExpression="UserBirth" />
                <asp:BoundField DataField="UserHobby" HeaderText="UserHobby" SortExpression="UserHobby" />
                <asp:BoundField DataField="UserAudit" HeaderText="UserAudit" SortExpression="UserAudit" />
                <asp:BoundField DataField="UserAddr" HeaderText="UserAddr" SortExpression="UserAddr" />
                <asp:TemplateField HeaderText="操作">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server"  CommandArgument='<%#Eval("UserID") %>' OnClick="LinkButton1_Click">显示数据</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" Text="选中删除" OnClick="Button1_Click"/>
    </div>
</asp:Content>

后台代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace 周五重点
{
    public partial class teacher : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GridView1.DataSource = BLL.Manager.usershow();
                this.GridView1.DataBind();
            }
        }
        //全选
        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkall = sender as CheckBox;
            //循环GridView1控件的每一个复选框
            foreach (GridViewRow item in this.GridView1.Rows)
            {
                if (item.RowType== DataControlRowType.DataRow)
                {
                    //类型转换
                    CheckBox chkone = item.Cells[0].FindControl("chkone") as CheckBox;
                    chkone.Checked = chkall.Checked;
                }
            }
        }
        //显示数据
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            LinkButton link = sender as LinkButton;
            int id = int.Parse(link.CommandArgument);
            Session["uid"] = id;
            Response.Redirect("teacher2.aspx");
        }
        //选中删除
        protected void Button1_Click(object sender, EventArgs e)
        {
            bool result=false;
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox chkone = this.GridView1.Rows[i].Cells[0].FindControl("chkone") as CheckBox;
                if (chkone.Checked)
                {
                    int id = int.Parse(this.GridView1.DataKeys[i].Value.ToString());
                    result = BLL.Manager.userDel(id);
                }
            }
            if (result)
            {
                this.GridView1.DataSource = BLL.Manager.usershow();
                this.GridView1.DataBind();
            }
          
        }
    }
}

标签:chkone,CheckBox,sender,母版,System,全选,using,精简,GridView1
来源: https://www.cnblogs.com/16699qq/p/13151123.html