ASP.Net GridView自带的删查改

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


恰饭广告




实现效果:

实现效果

BLL层:

using GridviewTest.DAL;
using GridviewTest.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
namespace GridviewTest.BLL
{
    public class UserInfoBLL
    {
        UserInfoDAL userInfoDAL = new UserInfoDAL();
        public DataTable LoadData()
        {
            return userInfoDAL.LoadData();
        }
        public void Delete(UserInfo user)
        {
            userInfoDAL.Delete(user);
        }
        public void UserUpdate(UserInfo user)
        {
            userInfoDAL.UserUpdate(user);
        }
    }
}

DAL层:

using GridviewTest.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace GridviewTest.DAL
{
    public class UserInfoDAL
    {
        SQLHelper sqlHelp = new SQLHelper();
        public DataTable LoadData()
        {
            string cmdText = "select * from T_login";
            return sqlHelp.ExecuteTable(cmdText);
        }
        public void Delete(UserInfo user)
        {
            sqlHelp.ExecuteNonQuery("delete from T_login where id=@id",
                new SqlParameter("id",user.id));
        }
        public void UserUpdate(UserInfo user)
        {
            sqlHelp.ExecuteNonQuery(@"UPDATE T_login SET username=@name,password=@pwd where id=@id",
                 new SqlParameter("@id", user.id),
                new SqlParameter("@name", user.name),
                new SqlParameter("@pwd", user.pwd));
        }
    }
}

Model层:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace GridviewTest.Model
{
    public class UserInfo
    {
        public int id { get; set; }
        public string name { get; set; }
        public string pwd { get; set; }
    }
}

UI层:

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GridviewTest.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" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                <Columns>
                    <asp:TemplateField HeaderText="学号">
                        <EditItemTemplate>
                            <asp:Label ID="Label2" Text='<%# Eval("id") %>' runat="server"></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" Text='<%# Eval("id") %>' runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="姓名">
                        <ItemTemplate>
                            <%# DataBinder.Eval(Container.DataItem,"username") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("username") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="密码">
                        <ItemTemplate>
                            <%# DataBinder.Eval(Container.DataItem,"password") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox2" Text='<%# Eval("password") %>' runat="server"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="操作">
                        <EditItemTemplate>
                            <asp:Button ID="btnUpdate" runat="server" Text="更新" CommandName="Update" />
                            <asp:Button ID="btnCancel" runat="server" Text="取消" CommandName="Cancel" />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Button ID="btnEdit" runat="server" Text="编辑" CommandName="Edit" />
                            <asp:Button ID="btnDel" runat="server" Text="删除" CommandName="Delete" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

WebForm1.aspx.cs

using GridviewTest.BLL;
using GridviewTest.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace GridviewTest
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        UserInfoBLL userInfoBLL = new UserInfoBLL();
        UserInfo user = new UserInfo();
         private void BindGridView()
        {
            GridView1.DataSource = userInfoBLL.LoadData();
            GridView1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridView();
            }
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            user.id = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text);
            userInfoBLL.Delete(user);
            BindGridView();
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            user.id = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("Label2")).Text);
            user.name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
            user.pwd = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
            userInfoBLL.UserUpdate(user);
            GridView1.EditIndex = -1;
            BindGridView();
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindGridView();
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindGridView();
        }
    }
}

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

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

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



恰饭广告

发表评论

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

2 × 2 =