效果展示:
代码:
Manager:
Add():添加方法
Select():数据库表查询方法
Del():删除方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
public class ArticleManager
{
MyDBEntities1 md = new MyDBEntities1();
public int Add(Article article)
{
md.Article.Add(article);
return md.SaveChanges();
}
public List<Article> Select()
{
return md.Article.Select(a => a).ToList();
}
public int Del(int id)
{
var FUCK = (from u in md.Article
where u.Id == id
select u).FirstOrDefault();
md.Article.Remove(FUCK);
return md.SaveChanges();
}
}
}
主页:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CK.aspx.cs" Inherits="WebApplication1.CK" %>
<!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>
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1">
<tr>
<td>编号</td>
<td>标题</td>
<td>作者</td>
<td>发布时间</td>
<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><%# Eval("PushTime") %></td>
<td><%# Eval("Contents") %></td>
<td><%# Eval("CatelogName") %></td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="view" CommandArgument='<%# Eval("Id") %>'>查看详情</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete" CommandArgument='<%# Eval("Id") %>'>删除</asp:LinkButton></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="去发布" />
</div>
</form>
</body>
</html>
主页CS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class CK : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["t"] != null)
{
Response.Write("<script>alert('添加成功!')</script>");
}
MyDBEntities1 db = new MyDBEntities1();
var result = from article in db.Article
join Catelog in db.Catelog on article.Catelogid equals Catelog.Id
select new { ID = article.Id, Title = article.Title, PushTime = article.PushTime, Contents = article.Contents, Author = article.Author, CatelogName = Catelog.Name };
this.Repeater1.DataSource = result.ToList();
this.Repeater1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Add.aspx");
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
int id = int.Parse(e.CommandArgument.ToString());
ArticleManager manager = new ArticleManager();
int count = manager.Del(id);
if (count > 0)
{
Response.Write("<script>alert('删除成功!')</script>");
MyDBEntities1 db = new MyDBEntities1();
var result = from article in db.Article
join Catelog in db.Catelog on article.Catelogid equals Catelog.Id
select new { ID = article.Id, Title = article.Title, PushTime = article.PushTime, Contents = article.Contents, Author = article.Author, CatelogName = Catelog.Name };
this.Repeater1.DataSource = result.ToList();
this.Repeater1.DataBind();
}
}
else
{
}
}
}
}
添加页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Add.aspx.cs" Inherits="WebApplication1.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 style="margin-left: 40px">
<h1>添加文章</h1>
标题:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
作者:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
正文:<asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine"></asp:TextBox>
<br />
<br />
类别:<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">测试</asp:ListItem>
<asp:ListItem Value="2">C#</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="发表文章" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="取 消" />
</div>
</form>
</body>
</html>
添加页面CS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Add : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Article article = new Article();
article.Title = this.TextBox1.Text;
article.Author = this.TextBox2.Text;
article.PushTime= DateTime.Now;
article.Contents = this.TextBox3.Text;
article.Catelogid = int.Parse(this.DropDownList1.Text);
ArticleManager manager = new ArticleManager();
int a = manager.Add(article);
if (a > 0 )
{
Response.Redirect("CK.aspx?t=t");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("CK.aspx");
}
}
}