aspx
<body style="font-family: 微软雅黑;"> <form id="form1" runat="server"> <div style=" 80%; margin-left: 10%; height: 250px; position: relative; border-bottom: 1px solid;" id="f"> <div style=" 470px; margin-left: 10%; margin-top: 20px; height: 220px; position: relative;"> <div style=" 60px; float: left;">姓名:</div> <input type="text" name="mname" placeholder="输入姓名" id="tt1" runat="server" /><span style="color: red;">必填</span><br /> <br /> <div style=" 60px; float: left;">电话:</div> <input type="text" name="tel" placeholder="输入电话" id="ph1" runat="server"/><span style="color: red;">必填</span><br /> <br /> <div style=" 60px; float: left;">e-mail:</div> <input type="text" name="mail" placeholder="输入邮箱" id="ma1" runat="server"/><br /> <br /> <div style=" 60px; float: left;">内容:</div> <textarea style=" 350px; height: 80px; max- 350px; max-height: 80px;" name="context" placeholder="输入内容" id="tt2" runat="server"></textarea><span style="color: red;">必填</span> <asp:Button ID="Button1" runat="server" Text="发表留言" style="margin-left:60px;" /> </div> </div> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <div style=" 80%; margin-left: 10%; height: 130px; background-color: #dddddd; position: relative; margin-top: 30px;"> <div style="float: left; 60px; height: 60px; margin-top: 35px; margin-left: 35px;"> <img src="~/pic/13.gif" width="60" height="60" /> </div> <div> <div style="margin-left: 15%;">姓名:<%#Eval("name") %></div> <div style=" 70%; margin-left: 15%; height: 80px; position: relative;">内容:<%#Eval("context") %></div> <div style="float: left; margin-left: 15%;">时间:<%#Eval("mtime") %></div> <div style="float: right; margin-right: 10%;"><a href="/home/delete?key=<%#Eval("ids") %>" style="color: red;">删除</a></div> </div> </div> </ItemTemplate> </asp:Repeater> </form> </body>
实体类 数据访问类
public class message { public message() { // // TODO: 在此处添加构造函数逻辑 // } public int ids { get; set; } public string name { get; set; } public string tel { get; set; } public string mail { get; set; } public string context { get; set; } public DateTime mtime { get; set; } }
public class messagedata { SqlConnection conn = null; SqlCommand cmd = null; public messagedata() { conn = new SqlConnection("server=.;database=aihu;user=sa;pwd=123"); cmd = conn.CreateCommand(); } public List<message> select() { List<message> mlist = new List<message>(); cmd.CommandText = "select * from message"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { message m = new message(); m.ids = Convert.ToInt32(dr["ids"]); m.name = dr["name"].ToString(); m.tel = dr["tel"].ToString(); m.mail = dr["mail"].ToString(); m.context = dr["context"].ToString(); m.mtime = Convert.ToDateTime(dr["mtime"]); mlist.Add(m); } } conn.Close(); return mlist; } public bool insert(message m) { int count = 0; bool ok = false; cmd.CommandText = "insert into message values(@a,@b,@c,@d,@e)"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@a",m.name); cmd.Parameters.AddWithValue("@b",m.tel); cmd.Parameters.AddWithValue("@c",m.mail); cmd.Parameters.AddWithValue("@d",m.context); cmd.Parameters.AddWithValue("@e",m.mtime); conn.Open(); count = cmd.ExecuteNonQuery(); conn.Close(); if (count > 0) { ok = true; } return ok; } }
后台cs:
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Repeater1.DataSource = new messagedata().select(); Repeater1.DataBind(); } Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e) { message m = new message(); m.name = tt1.Value; m.tel = ph1.Value; m.mail = ma1.Value; m.context = tt2.Value; m.mtime = DateTime.Now; bool ok = new messagedata().insert(m); Repeater1.DataSource = new messagedata().select(); Repeater1.DataBind(); } }