标签:
所用到的方法
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(); } }
发布页面
代码
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> 发布新闻<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> 发布新闻<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"); } }
标签:
原文地址:http://www.cnblogs.com/zxm1002/p/4949974.html