ASP.Net GridView多选删除

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




实现效果:

实现效果

数据库表:

数据库表内容

项目的数据模块图:

项目结构图

BLL层:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApplication4.DAL;
using WebApplication4.Model;
namespace WebApplication4.BLL
{
    public class UserInfoBLL
    {
        UserInfoDAL userDal = new UserInfoDAL();
        public object LoadGetAllData()
        {
            object all = userDal.GetAllData();
            return all;
        }
        public string DelById(UserInfo user)
        {
           return userDal.DelById(user);
        }
        public int Count()
        {
            int count = userDal.count;
            return count;
        }
    }
}

DAL层:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using WebApplication4.Model;
namespace WebApplication4.DAL
{
    public class UserInfoDAL
    {
        SQLHelper sqlhelp = new SQLHelper();
        private UserInfo ToUserInfo(DataRow row)
        {
            UserInfo user = new UserInfo();
            user.Id = (int)row["Id"];
            user.Name = (string)row["Name"];
            user.Sex = (string)row["Sex"];
            user.Age = (int)row["Age"];
            user.Department = (string)row["Department"];
            user.BirthDay = (DateTime?)(row["BirthDay"]);
            user.HireDate = (DateTime?)(row["HireDate"]);
            user.TelNum = (string)row["TelNum"];
            user.Address = (string)row["Address"];
            return user;
        }
        public UserInfo[] GetAllData()
        {
            DataTable table = sqlhelp.ExecuteDataTable("select * from T_UserInfo");
            UserInfo[] user = new UserInfo[table.Rows.Count];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow row = table.Rows[i];
                user[i] = ToUserInfo(row);
            }
            return user;
        }
        public int count;
        public string DelById(UserInfo user)
        {
            string s = sqlhelp.ExecuteSqlNonQuery("delete T_UserInfo where Id in " + user.delId).ToString();
            count = Convert.ToInt32(s);
            return s;
        }
    }
}

Model层(Entity类):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication4.Model
{
    public class UserInfo
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Sex { get; set; }
        public int Age { get; set; }
        public string Department { get; set; }
        public DateTime? BirthDay { get; set; }
        public DateTime? HireDate { get; set; }
        public string TelNum { get; set; }
        public string Address { get; set; }
        public string delId { get; set; }
    }
}

UI层:

WebForm1.aspx代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
                <Columns>
                    <asp:TemplateField HeaderText="选择">
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="序号">
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server"  Text="<%# Container.DataItemIndex+1 %>"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Id" HeaderText="编号" />
                    <asp:BoundField DataField="Name" HeaderText="姓名" />
                    <asp:BoundField DataField="Sex" HeaderText="性别" />
                    <asp:BoundField DataField="Age" HeaderText="年龄" />
                    <asp:BoundField DataField="Department" HeaderText="部门" />
                    <asp:BoundField DataField="BirthDay" HeaderText="出生日期" DataFormatString="{0:yyyy-MM-dd}" />
                    <asp:TemplateField HeaderText="入职时间">
                        <ItemTemplate>
                            <asp:Label ID="Label4" runat="server" Text='<%# Eval("HireDate", "{0:yyyy-MM-dd}") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="TelNum" HeaderText="手机号码" />
                    <asp:BoundField DataField="Address" HeaderText="详细地址" />
                    <asp:TemplateField HeaderText="操作">
                        <ItemTemplate>
                            <asp:Button ID="btnEdit" runat="server" OnClick="Button1_Click" OnClientClick="return showConfirm(this);" Text="编辑" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
        <asp:CheckBox ID="cboCheck" runat="server" AutoPostBack="True" OnCheckedChanged="cboCheck_CheckedChanged" Text="全选" />
        <asp:Button ID="btnCheck" runat="server" OnClick="btnCheck_Click" Text="反选" />
        <asp:Button ID="getValue" runat="server" OnClick="getValue_Click" Text="取值" />
        <asp:Label ID="Label3" runat="server"></asp:Label>
        <asp:Button ID="btnMulDel" runat="server" OnClick="btnMulDel_Click"  Text="批量删除" />
    </form>
</body>
    <script type="text/javascript" src="https://idaobin.com/test/jquery-3.2.1.js"></script>
        <script>
        function showConfirm(btn) {
            var stuNO = $(btn).parent().prev().prev().prev().prev().prev().prev().prev().prev().prev().text();
            return window.confirm("您选中了【"+stuNO+"】的学生信息吗");
        }
    </script>
</html>

WebForm1.aspx.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication4.BLL;
using WebApplication4.Model;
namespace WebApplication4
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        UserInfoBLL userBll = new UserInfoBLL();
        UserInfo user = new UserInfo();
        public void LoadAllData()
        {
            GridView1.DataSource = userBll.LoadGetAllData();
            GridView1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadAllData();
            }
            btnMulDel.Attributes["OnClick"] = "return confirm('是否删除记录?')";
        }
        protected void cboCheck_CheckedChanged(object sender, EventArgs e)
        {
            for (int i=0;i<GridView1.Rows.Count;i++)
            {
                CheckBox cbCheck = GridView1.Rows[i].FindControl("CheckBox1") as CheckBox ;
                if (cboCheck.Checked==true)
                {
                    cbCheck.Checked = true;
                }
                else
                {
                    cbCheck.Checked = false;
                }
            }
        }
        protected void btnCheck_Click(object sender, EventArgs e)
        {
            int rowCount = this.GridView1.Rows.Count;
            for (int i = 0; i < rowCount; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                CheckBox cbCheck = row.FindControl("CheckBox1") as CheckBox;
                cbCheck.Checked = !cbCheck.Checked;
                //cbCheck.Checked = true; //全选
                //cbCheck.Checked = false; //全不选
            }
        }
        protected void getValue_Click(object sender, EventArgs e)
        {
            int rowCount = this.GridView1.Rows.Count;
            string str = "";
            for (int i = 0; i < rowCount; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                CheckBox cbCheck = row.FindControl("CheckBox1") as CheckBox;
                if (cbCheck.Checked)
                {
                    str += row.Cells[3].Text + ",";
                }
                string s = str.TrimEnd(',');
                Label3.Text = s;
            }
        }
        protected void btnMulDel_Click(object sender, EventArgs e)
        {
            string sqlText = "(";
            foreach (GridViewRow objGVR in this.GridView1.Rows)
            {
                //判断当前行是否为数据行;
                if (objGVR.RowType == DataControlRowType.DataRow)
                {
                    CheckBox objCB = objGVR.FindControl("CheckBox1") as CheckBox;
                    if (objCB.Checked)
                    {
                        //获取选中行的主键;
                        sqlText += this.GridView1.DataKeys[objGVR.RowIndex]["id"].ToString() + ",";
                    }
                }
            }
            sqlText = sqlText.Substring(0, sqlText.Length - 1) + ")";
            if (sqlText==")")
            {
                Response.Write("没有选中任何记录");
            }
            else
            {
                user.delId = sqlText;
                userBll.DelById(user);
                LoadAllData();
                Response.Write("已删除" + userBll.Count() + "条记录");
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Button btnDel = sender as Button;
            GridViewRow row = btnDel.Parent.Parent as GridViewRow;
            int stuNO = Convert.ToInt32(row.Cells[2].Text);
            Response.Write(stuNO);
        }
    }
}

原文链接:https://www.idaobin.com/archives/1293.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

1 + 9 =