.Net 表+下拉框(筛选)+删除+修改

Mondels


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
    public class BookInfo
    {
        //prop 2次tab键
        public int Id { get; set; }
        public string  Name { get; set; }
        public string Author { get; set; }
        public double Price { get; set; }

        public DateTime AddTime{ get; set; }
        public int CId { get; set; }
        public int Status { get; set; }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
    public class Category
    {
        public int CId { get; set; }
        public string Name { get; set; }
    }
}

DAL

using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class BookInfoService
    {
        //1.查询所有图书信息
        public static List<BookInfo> GetBookInfos()
        {
            //查询语句
            string sql = "select * from BookInfo";
            //调用DBHelper查询,返回数据表
            DataTable dt=DBHelper.ExecuteTable(sql);
            //创建集合存储对象数据
            List<BookInfo> books = new List<BookInfo>();
            //遍历dt数据表,将对象添加到集合中
            foreach (DataRow dr in dt.Rows)
            {
                //创建对象存放数据
                BookInfo book = new BookInfo();
                book.Id = int.Parse(dr[0].ToString());
                book.Name = dr[1].ToString();
                book.Author = dr[2].ToString();
                book.Price = double.Parse(dr[3].ToString());
                book.AddTime = DateTime.Parse(dr[4].ToString());
                book.CId = int.Parse(dr[5].ToString());
                book.Status = int.Parse(dr[6].ToString());
                //注意添加对象到集合中
                books.Add(book);
            }
            return books;//返回集合
        }
        //2.查询类别信息
        public static List<Category> GetCategories()
        {
            //查询类别表
            string sql = "select * from Category";
            DataTable dt=DBHelper.ExecuteTable(sql);
            List<Category> list = new List<Category>();
            foreach (DataRow dr in dt.Rows)
            {
                Category c = new Category();
                c.CId =int.Parse( dr[0].ToString());
                c.Name = dr[1].ToString();
                list.Add(c);
            }
            return list;
        }
        //3.根据ID查询图书信息
        public static List<BookInfo> GetBookInfosByID(int id)
        {
            string sql = "select * from BookInfo where Cid=" + id;
            if (id==0)
            {
                sql="select * from BookInfo";
            }
            DataTable dt = DBHelper.ExecuteTable(sql);
            List<BookInfo> books = new List<BookInfo>();
            foreach (DataRow dr in dt.Rows)
            {
                BookInfo book = new BookInfo();
                book.Id = int.Parse(dr[0].ToString());
                book.Name = dr[1].ToString();
                book.Author = dr[2].ToString();
                book.Price = double.Parse(dr[3].ToString());
                book.AddTime = DateTime.Parse(dr[4].ToString());
                book.CId = int.Parse(dr[5].ToString());
                book.Status = int.Parse(dr[6].ToString());
                //注意添加对象到集合中
                books.Add(book);
            }
            return books;
        }
        //4.删除
        public static int Delete(int id)
        {
            //删除语句
            string sql = "delete from BookInfo where id=" + id;
            return DBHelper.ExecuteQuery(sql);//返回受影响行数
        }
        //5.修改状态
        public static int Update(int id)
        {
            //修改语句
            string sql = "update  BookInfo set Status=1 where id=" + id;
            return DBHelper.ExecuteQuery(sql);//返回受影响行数
        }
    }
}

BLL

using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;

namespace BLL
{
    public class BookInfoManage
    {
        /// <summary>
        /// 显示表数据
        /// </summary>
        /// <returns></returns>
        public static List<BookInfo> GetBookInfos()
        {
            return BookInfoService.GetBookInfos();
        }
       /// <summary>
       /// 下拉框数据
       /// </summary>
       /// <returns></returns>
        public static List<Category> GetCategories()
        {
            return BookInfoService.GetCategories();
        }
        /// <summary>
        /// 下拉框搜索
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<BookInfo> GetBookInfosByID(int id)
        {
            return BookInfoService.GetBookInfosByID(id);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int Delete(int id)
        {
            return BookInfoService.Delete(id);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int Update(int id)
        {
            return BookInfoService.Update(id);
        }
    }
}

UI

Table

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="BookUI.Index" %>

<!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:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >

            </asp:DropDownList>
            <table border="1" cellpadding="0" cellspacing="0">
                <tr>
                    <td>书号</td>
                    <td>书名</td>
                    <td>作者</td>
                    <td>价格</td>
                    <td>添加时间</td>
                    <td>状态</td>
                    <td>编辑</td>
                </tr>
                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <%--ctrl+K+F代码格式化--%>
                        <tr>
                            <td><%#Eval("Id") %></td>
                            <td><%#Eval("Name") %></td>
                            <td><%#Eval("Author") %></td>
                            <td><%#Eval("Price") %></td>
                            <td><%#Eval("AddTime") %></td>     
                            <%--三元运算符 条件表达式?表达式1:表达式2--%>
                            <td><%# Convert.ToInt32(Eval("Status"))==0?"未借阅":"已借阅" %></td>
                            <td>
                                <asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandArgument='<%#Eval("Id") %>' OnClientClick="return confirm('是否确认删除?')">删除</asp:LinkButton>

                                <asp:LinkButton ID="LinkButton2" runat="server" OnCommand="LinkButton2_Command" CommandArgument='<%#Eval("Id") %>'>借阅</asp:LinkButton>
                            </td>
                        </tr>
                    </ItemTemplate>

                </asp:Repeater>
            </table>
        </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 Models;
using BLL;

namespace BookUI
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Show();
            }
        }

        private void Show()
        {
           //绑定图书信息
            this.Repeater1.DataSource = BookInfoManage.GetBookInfos();
            this.Repeater1.DataBind();

            //绑定下拉框数据
            this.DropDownList1.DataSource = BookInfoManage.GetCategories();
            this.DropDownList1.DataTextField = "Name";
            this.DropDownList1.DataValueField = "CId";
            this.DropDownList1.DataBind();
            this.DropDownList1.Items.Insert(0,new ListItem("所有","0"));//添加"所有"项
        }

        //下拉框搜索功能
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取ID
            int id = this.DropDownList1.SelectedIndex;
            //绑定数据源
            this.Repeater1.DataSource=BookInfoManage.GetBookInfosByID(id);
            this.Repeater1.DataBind();
        }

        //删除
        protected void LinkButton1_Command(object sender, CommandEventArgs e)
        {
            //获取id
            int id =int.Parse( e.CommandArgument.ToString());
            int c =BookInfoManage.Delete(id);//获取受影响行数
            if (c>0)
            {
                //弹窗提示
                Response.Write("<script>alert('删除成功!')</script>");
                //刷新数据
                Show();
            }
        }

        //修改状态
        protected void LinkButton2_Command(object sender, CommandEventArgs e)
        {
            //获取id
            int id = int.Parse(e.CommandArgument.ToString());
            BookInfoManage.Update(id);//更新数据库
            Show();//刷新数据
        }
    }
}

GridView1

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UI.aspx.cs" Inherits="UI_3._2fuxi.UI" %>

<!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:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

            </asp:DropDownList>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal">
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="编号" />
                    <asp:BoundField DataField="Name" HeaderText="书名" />
                    <asp:BoundField DataField="Author" HeaderText="作者" />
                    <asp:BoundField DataField="Price" HeaderText="价格" />
                    <asp:BoundField DataField="AddTime" HeaderText="发布时间" />

                    <asp:TemplateField HeaderText="状态">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server"
                                Text='<%#Convert.ToInt32(Eval("Status"))==0?"未借阅":"已借阅" %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField ShowHeader="False" HeaderText="编辑">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server"
                                CommandArgument='<%#Eval("Id") %>' OnCommand="LinkButton1_Command" OnClientClick="return confirm('是否确认删除?')">
                                删除</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField ShowHeader="False" HeaderText="借阅">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton2" runat="server" 
                                CommandArgument='<%#Eval("Id") %>' OnCommand="LinkButton2_Command" OnClientClick="return confirm('是否借阅?')">
                                借阅</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>

                </Columns>
                <FooterStyle BackColor="White" ForeColor="#333333" />
                <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="White" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F7F7F7" />
                <SortedAscendingHeaderStyle BackColor="#487575" />
                <SortedDescendingCellStyle BackColor="#E5E5E5" />
                <SortedDescendingHeaderStyle BackColor="#275353" />
            </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 Models;
using BLL;

namespace UI_3._2fuxi
{
    public partial class UI : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Show();
                Short();
            }
        }

        private void Short()
        {
            this.DropDownList1.DataSource = CategoryMoanage.GetCtaegories();
            this.DropDownList1.DataTextField = "Name";
            this.DropDownList1.DataValueField = "CId";
            this.DropDownList1.DataBind();
            this.DropDownList1.Items.Insert(0, new ListItem("所有", "0"));
        }

        //展示
        private void Show()
        {
            this.GridView1.DataSource = BookInfoManage.GetBookInfos();
            this.GridView1.DataBind();         
        }


        //下拉框搜索
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int CId = this.DropDownList1.SelectedIndex;
            this.GridView1.DataSource = BookInfoManage.GetBookInfos(CId);
            this.GridView1.DataBind();
        }

        protected void LinkButton1_Command(object sender, CommandEventArgs e)
        {
            int Id = int.Parse(e.CommandArgument.ToString());
            int c = BookInfoManage.Del(Id);
            if (c>0)
            {
                Response.Write("<script>alert('删除成功!')</script>");
                Show();
            }
        }

        protected void LinkButton2_Command(object sender, CommandEventArgs e)
        {
            int Id = int.Parse(e.CommandArgument.ToString());
            int c = BookInfoManage.Update(Id);
            if (c > 0)
            {
                Response.Write("<script>alert('借阅成功!')</script>");
                Show();
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值