简单新闻发布系统
所用到的方法
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient;/// <summary> /// news 的摘要說明 /// </summary> public class newsDA {private DataClassesDataContext Context;private SqlConnection conn;private SqlCommand cmd;public newsDA(){//// TODO: 在此處添加構造函數邏輯// Context = new DataClassesDataContext();}public void Insert(news data){Context.news.InsertOnSubmit(data);Context.SubmitChanges();}public void updata(news data){ //先去模型中找news sdata = Context.news.Single(r => r.newsid == data.newsid);//找到后修改sdata.newsid = data.newsid;sdata.title = data.title;sdata.Author = data.Author;sdata.source = data.source;sdata.content = data.content;sdata.time = data.time;//提交修改 Context.SubmitChanges();}public List<news> select(){return Context.news.ToList();}public news select(string ids){return Context.news.Where(r => r.newsid == int.Parse(ids)).First();}public List<news> selebytitle(string name){return Context.news.Where(r => r.title == name).ToList();}public void delete(int ids){news data = Context.news.Single(r=>r.newsid==ids);Context.news.DeleteOnSubmit(data);Context.SubmitChanges();} }?
?
發(fā)布頁面
代碼
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e){}protected void Button2_Click(object sender, EventArgs e){Response.Redirect("select.aspx");}protected void Button1_Click(object sender, EventArgs e){news data = new news();DateTime time = DateTime.Now;data.title = TextBox1.Text;data.Author = TextBox2.Text;data.source = TextBox3.Text;data.content = TextBox4.Text;data.time = time;new newsDA().Insert(data);} }?
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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>發(fā)布新聞<br /> 標題:<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:TextBox ID="TextBox4" runat="server" Height="130px" Width="136px"></asp:TextBox><br /> <asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" /><asp:Button ID="Button2" runat="server" Text="查看" OnClick="Button2_Click" /></div> </form> </body> </html>查看內容
代碼
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;public partial class select : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e){Repeater1.DataSource = new newsDA().select();Repeater1.DataBind();//綁定數據 } }HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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>發(fā)布新聞<br />標題:<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:TextBox ID="TextBox4" runat="server" Height="130px" Width="136px"></asp:TextBox><br /><asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" /><asp:Button ID="Button2" runat="server" Text="查看" OnClick="Button2_Click" /></div></form> </body> </html>修改內容
代碼
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;public partial class insert : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){if (Request["id"] != null){string ids = Request["id"].ToString();news data = new newsDA().select(ids);DateTime time = DateTime.Now;TextBox1.Text = data.title;TextBox2.Text = data.Author;TextBox3.Text = data.source;TextBox4.Text = data.content;time = data.time;Label1.Text = data.newsid.ToString();}}}protected void Button1_Click(object sender, EventArgs e){news data = new news();DateTime time = DateTime.Now;data.title = TextBox1.Text;data.Author = TextBox2.Text;data.source = TextBox3.Text;data.content = TextBox4.Text;data.time = time;data.newsid =int.Parse( Label1.Text);new newsDA().updata (data);}protected void Button2_Click(object sender, EventArgs e){Response.Redirect("select.aspx");} }HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="updata.aspx.cs" Inherits="insert" %><!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" Visible="False"></asp:Label><br />標題:<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:TextBox ID="TextBox4" runat="server"></asp:TextBox><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>刪除內容,新建一個web窗體,什么都不用寫
代碼
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;public partial class delete : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e){string ids=Request["id"].ToString();new newsDA().delete(int.Parse(ids));Response.Redirect("select.aspx");} }?
轉載于:https://www.cnblogs.com/zxm1002/p/4949974.html
總結
- 上一篇: 配置使用EF6.0常见的一些问题及解决方
- 下一篇: Ubuntu 安装 Sun JDK