2021-05-28

1.物理数据库的构建及外键关系

2.将物理数据库通过EF框架进行映射,为后面web页面的增删改查提供数据源

  a.新建ef


  b.建立与物理数据库的连接

  c.选择映射的表

2.写添加文章页面

a.前端

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

<!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:Label ID="Label1" runat="server" Text="标题"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
            <asp:Label ID="Label2" runat="server" Text="作者"></asp:Label><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
            <asp:Label ID="Label3" runat="server" Text="正文"></asp:Label><asp:TextBox ID="TextBox3" runat="server" Height="54px" TextMode="MultiLine" Width="202px"></asp:TextBox><br />
            <asp:Label ID="Label4" runat="server" Text="类型"></asp:Label><asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><br />
            <asp:Button ID="Button2" runat="server" Text="新增" OnClick="Button2_Click" />
        </div>
    </form>
</body>
</html>

b.后台显示代码

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

namespace WebApplication2
{
    public partial class Add : System.Web.UI.Page
    {
        DB1902Entities db = new DB1902Entities();
        protected void Page_Load(object sender, EventArgs e)
        {
            var result = (from a in db.Catelog select a).ToList();
            if (!IsPostBack)
            {
                this.DropDownList1.DataSource = result;
                this.DropDownList1.DataTextField = "Name";
                this.DropDownList1.DataValueField = "Id";
                this.DropDownList1.DataBind();
                this.DropDownList1.Items.Insert(0, new ListItem("全部", "0"));
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string title = this.TextBox1.Text;
            string author = this.TextBox2.Text;
            string context = this.TextBox3.Text;
            int type = int.Parse(this.DropDownList1.SelectedValue);
            DateTime time = DateTime.Now;
            
            if (type == 0)
            {
                Response.Write($"<script>alert('请选择类型')</script>");
            }
            else
            {
                Article a = new Article();
                a.Title = title;
                a.PushTime = time;
                a.Content = context;
                a.Catelogid = type;
                a.Author = author;
                db.Article.Add(a);
                db.SaveChanges();
                Response.Redirect("WebForm1.aspx");
            }
        }
    }
}

3.写显示文章的页面

a.前端

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.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:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                <asp:ListItem>请选择</asp:ListItem>
            </asp:DropDownList>

            <table>
                <tr>
                    <td>编号</td>
                    <td>标题</td>
                    <td>作者</td>
                    <td>详情</td>
                </tr>
            <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                <ItemTemplate>
                    <tr>
                        <td><%# Eval("id") %></td>
                        <td><%# Eval("title") %></td>
                        <td><%# Eval("author") %></td>
                        <td><asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("id") %>' CommandName="xq">详情</asp:LinkButton>
                            <asp:LinkButton ID="LinkButton2" runat="server" CommandArgument='<%# Eval("id") %>' CommandName="xg">修改</asp:LinkButton>
                        </td>
                        
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
            </table>
        </div>
    </form>
</body>
</html>

b.后端代码的攥写

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

namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        DB1902Entities db = new DB1902Entities();
        protected void Page_Load(object sender, EventArgs e)
        {      
            
            var result = from a in db.Article select a;
            var result2 = (from a in db.Catelog select a).ToList();

            if (!IsPostBack)
            {
                this.Repeater1.DataSource = result.ToList();
                this.Repeater1.DataBind();
                this.DropDownList1.DataSource = result2;
                this.DropDownList1.DataTextField = "Name";
                this.DropDownList1.DataValueField = "Id";
                this.DropDownList1.DataBind();
                this.DropDownList1.Items.Insert(0, new ListItem("全部", "0"));
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = int.Parse(this.DropDownList1.SelectedValue);
            var result = from a in db.Article where a.Catelogid == id select a;
            this.Repeater1.DataSource = result.ToList();
            this.Repeater1.DataBind();
            if (id == 0)
            {
                var result2 = from a in db.Article select a;
                this.Repeater1.DataSource = result2.ToList();
                this.Repeater1.DataBind();
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            
            if (e.CommandName == "xq")
            {
                int id = int.Parse(e.CommandArgument.ToString());
                Response.Redirect($"xiangq.aspx?id={id}");
            }
            else
            {
                int id = int.Parse(e.CommandArgument.ToString());
                Response.Redirect($"Update.aspx?id={id}");
            }
            
        }


    }
}

3.写修改页面

a.前端

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

<!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:Label ID="Label1" runat="server" Text="标题"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
            <asp:Label ID="Label2" runat="server" Text="作者"></asp:Label><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
            <asp:Label ID="Label3" runat="server" Text="正文"></asp:Label><asp:TextBox ID="TextBox3" runat="server" Height="54px" TextMode="MultiLine" Width="202px"></asp:TextBox><br />
            <asp:Label ID="Label4" runat="server" Text="类型"></asp:Label><asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><br />
            <asp:Button ID="Button2" runat="server" Text="修改" OnClick="Button2_Click" />
        </div>
    </form>
</body>
</html>

b.后端

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

namespace WebApplication2
{
    public partial class Update : System.Web.UI.Page
    {
        DB1902Entities db = new DB1902Entities();
        DateTime time ;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            var result2 = (from a in db.Catelog select a).ToList();
            if (!IsPostBack)
            {
                int id = int.Parse(Request["id"].ToString());
                Article result = db.Article.FirstOrDefault(p => p.id == id);

                
                
                this.TextBox1.Text = result.Title;
                this.TextBox2.Text = result.Author;
                this.TextBox3.Text = result.Content;
                this.DropDownList1.SelectedValue = result.Catelogid.ToString();
                time = DateTime.Parse(result.PushTime.ToString());



                this.DropDownList1.DataSource = result2;
                this.DropDownList1.DataTextField = "Name";
                this.DropDownList1.DataValueField = "Id";
                this.DropDownList1.DataBind();
                this.DropDownList1.Items.Insert(0, new ListItem("全部", "0"));
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int id = int.Parse(Request["id"].ToString());
            Article result = db.Article.FirstOrDefault(p => p.id == id);
            string Title = this.TextBox1.Text;
            string author = this.TextBox2.Text;
            string content = this.TextBox3.Text;
            int cid = int.Parse(this.DropDownList1.SelectedValue.ToString());
            result.Title = Title;
            result.Author = author;
            result.Content = content;
            result.Catelogid = cid;
            db.Entry(result).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
            Response.Redirect("WebForm1.aspx");
        }
    }
}

4.创建详情页面

a.前端

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

<!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:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        </div>
    </form>
</body>
</html>

b.后台

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

namespace WebApplication2
{
    public partial class xiangq : System.Web.UI.Page
    {
        DB1902Entities db = new DB1902Entities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id = int.Parse(Request["id"].ToString());
                var result = db.Article.FirstOrDefault(P => P.id == id);

                this.Label1.Text = result.Title;
                this.Label2.Text = result.Content;
            }
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值