2021-05-27

创建数据库MyDB
表一(Catelog)
在这里插入图片描述
添加几条数据在这里插入图片描述

表二(Article)
Catelogid设置为外键
在这里插入图片描述
添加几条数据在这里插入图片描述
添加Web层,Model层,DAL层,BLL层。

DAL引用Model,BLL引用Model、DAL,Web引用Model、BLL。

在这里插入图片描述
在Model层中添加DAO.NET实体数据模型
在这里插入图片描述
结果图
在这里插入图片描述

显示表代码

在DAL层中添加ArticleDAO类

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

namespace DAL
{
 public class ArticleDAO
    {
     MyDBEntities db = new MyDBEntities(); 
     public List<Article> Select()
        {
    
     //查询函数
     return db.Article.Select(p => p).ToList();
        }
        //在Article表中根据Id查询
        public Article Select(int id)
        {
        
            var result =(from article in db.Article
            where article.Id==id
                        select article).Single();
                        return result;
        }
        //添加方法
        public int Add(Article article)
        {
        db.Article.Add(article);
        //把所需添加的内容添加至数据库中,保存所写的东西
         int count = db.SaveChanges();
            return count;
            }
            //删除方法
            public int Remove(Article article)
        {
        db.Article.Remove(article);
            int count = db.SaveChanges();
            return count;
            }
            //修改方法
            public int Update(Article article)
        {
        //设置对象状态
        db.Entry<Article>(article).State = System.Data.Entity.EntityState.Modified;
            int count = db.SaveChanges();
            return count;
        }
        }
            }

在BLL层添加ArticleService类

```cpp
using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
public class ArticleService
    {
      //添加一个私有字段,用于调数据访问对象
        public ArticleDAO aAdo = new ArticleDAO();
        //添加文章
        public int AddArticle(Article article)
        {
            return aAdo.Add(article);
        }
        //查询所有
        public List<Article> Select()
        {
            return aAdo.Select();
        }
          //根据ID查询所有
        public Article Select(int id)
        {
            return aAdo.Select(id);
        }
                   //添加文章
        public int Add(Article article)
        {
            return aAdo.Add(article);
        }
        }
        }

在WebForm1.aspx中添加GridView控件,添加数据源。
在这里插入图片描述
并添加各列信息名称,添加button控件,并加按钮事件
在这里插入图片描述
综合代码如下

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm2" %>
<!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" DataSourceID="ObjectDataSource1">
                 <Columns>
                  <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
                     <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                   <asp:BoundField DataField="Author" HeaderText="Author" SortExpression="Author" />
                     <asp:BoundField DataField="Content" HeaderText="Content" SortExpression="Content" />
                     <asp:BoundField DataField="Catelogid" HeaderText="Catelogid" SortExpression="Catelogid" />
                    <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
                 </Columns>
</asp:GridView>
             <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select" TypeName="BLL.AritcleService"></asp:ObjectDataSource>
           <asp:Button ID="Button1" runat="server" Text="发表文章" OnClick="Button1_Click"/>
         </div>
          </form>
</body>
</html>

WebForm1.aspx.cs代码

using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication4
{
 public partial class WebForm2 : System.Web.UI.Page
    {
     AritcleService aritcleService = new AritcleService();
        protected void Page_Load(object sender, EventArgs e)
        {
        if (!IsPostBack)
            { 
                //定义一个msg接收添加页面传过来的msg
                string msg = Request.QueryString["msg"];
                if (!string.IsNullOrEmpty(msg))
                {
                 Response.Write("<script>alert('添加成功!')</script>");
                }
            }
            }
            protected void Button1_Click(object sender, EventArgs e)
        {
         Response.Redirect("Add.aspx");
        }
        }
        }

结果图
在这里插入图片描述
在DAL层添加CatelogDAO类

using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class CatelogDAO
    {
     //查询所有的
        public List<Catelog> Select()
        {
         MyDBEntities db = new MyDBEntities();
            var result = from catelog in db.Catelog
                     select catelog;
            return result.ToList();
 }
    }
}

在BLL层添加CatelogService类

using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
public  class CatelogService
    {
     private CatelogDAO cdao = new CatelogDAO();
        //查询所有的
         public List<Catelog> Select()
        {
            return cdao.Select();
        }
         }
}

在Web层添加Add.aspx窗体,并添加发表文章按钮,加事件。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Add.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:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />
            作者:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <br />
             文章内容:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            <br />
             类别:<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="name" DataValueField="id"></asp:DropDownList>
            <br />
             <asp:Button ID="Button1" runat="server" Text="发表文章" OnClick="Button1_Click" />
        </div>
        </form>
</body>
</html>

Add.aspx.cs代码,

using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
    {
     private CatelogService catelogService = new CatelogService();
        private AritcleService aritcleService = new AritcleService();
       protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            this.DropDownList1.DataSource = catelogService.Select();
                this.DropDownList1.DataBind();
            }
            }
             protected void Button1_Click(object sender, EventArgs e)
        {
            Article article = new Article();
           article.Title = this.TextBox1.Text;
            article.Author = this.TextBox2.Text;
             article.Content = this.TextBox3.Text;
            article.Catelogid = int.Parse(this.DropDownList1.SelectedValue);
             article.Date = DateTime.Now;
            //声明一个变量来接收执行后的结果
            int result= aritcleService.Add(article);
            //判断是否添加成功
            if (result > 0)
           {
                //成功了就执行这段代码
                //~/zhuye.aspx这是跳转到的那个页面  ?msg=添加成功给zhuye的msg赋值
       Response.Redirect("~/zhuye.aspx?msg=添加成功");
            }
            else
             {
                Response.Write("<script>alert('添加失败')</script>");
            }   
                 //if (result>0)
            //{
             //    Response.Redirect("WebForm1.aspx");
            //}
            }
            }
            }
            

结果图
点击页面中的发表按钮,跳转添加页面。
在这里插入图片描述
弹出添加成功
在这里插入图片描述
点击确定,跳回第一个界面,查看是否添加成功。
在这里插入图片描述
创建Web窗体WebForm3.aspx

在WebForm1.aspx中添加HyperLinkField,实现点击详情跳转详情页面功能。
在这里插入图片描述
并添加相关属性

 <%-- DataNavigateUrlFields = "Id"//绑定到超链接的属性字段
          DataNavigateUrlFormatString = "WebForm3.aspx?Id = {0}" />//链接属性应用的设置,即点击链接后转跳到Details.aspx--%>
 <asp:HyperLinkField DataNavigateUrlFields="Id" Text="详情"  DataNavigateUrlFormatString="WebForm3.aspx?Id={0}" />

效果图。
在这里插入图片描述
点击详情,跳转至内容界面。
在这里插入图片描述

在ArticleDAO类中添加根据类别查询

  //根据类别进行查询
        public List<Article> SelectByCatelogId(int id)
        {
            MyDBEntities1 db = new MyDBEntities1();
            var result = from article in db.Article
                         where article.Catelogid == id
                         select article;
            return result.ToList();
        }

在ArticleService中添加对应类别查询代码

        public List<Article> SelectByCatelogId(int id)
        {
            return articleDAO.SelectByCatelogId(id);
        }

新添加一个Web窗体,WenForm3.aspx
添加下拉框DropDownList控件,添加属性AutoPostBack为Ture,以及OnSelectedIndexChanged事件,并在下拉框控件添加数据值。添加Repeater。以及喜欢的样式代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication12.WebForm3" %>
<!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>
    <style>
        div{
            border:1px solid red;
              }
        .row{
           display:flex;
            flex-direction:row;
             }
        .cols-6{
            flex:0 0 50%;
               }
        label.cols-6{
            text-align:right;
        }
         </style>
</head>
<body>
    <form id="form1" runat="server">
      <div>
            <div class="row">
                <label class="cols-6">类别</label>
                <div class="cols-6">
                <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Name" DataValueField="Id" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
  <asp:ListItem Value="情感"></asp:ListItem>
                <asp:ListItem Value="小说"></asp:ListItem>
                 <asp:ListItem Value="技术"></asp:ListItem>
                <asp:ListItem Value="杂谈"></asp:ListItem>
  </asp:DropDownList>
                </div>
            </div>
             <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
            <div class="row">
                    <div class="row"><%#Eval("Title") %></div>
          <div class="row">
                 <label class="cols-6">作者<%#Eval("Author") %></label>
                        <label class="cols-6">日期<%#Eval("Date") %></label>
          </div>
                <div class="row"><%#Eval("Content") %></div>           
                </div>
                               
             </ItemTemplate>
        </asp:Repeater>
                </div>
                 </form>
</body>
</html>                        

WebForm3.aspx.cs代码如下。

using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication12
{
 public partial class WebForm3 : System.Web.UI.Page
    {
 CatelogService cs = new CatelogService();
        ArticleService sa = new ArticleService();
         protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
             {
                this.DropDownList1.DataSource = cs.Select();
                this.DropDownList1.DataBind();
                this.Repeater1.DataSource = sa.Select();
this.Repeater1.DataBind();
   }
        }
         protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
 int id = int.Parse(this.DropDownList1.SelectedValue);
                this.Repeater1.DataSource = sa.SelectByCatelogId(id);
                     this.Repeater1.DataBind();
        }           
      }
}        

结果图。
在这里插入图片描述
根据类别显示对应内容。
在这里插入图片描述
以下类别为小说在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值