<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="QQzone.aspx.cs" Inherits="WebApplication1.QQzone" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> #main { width:900px; color:Orange; font-size:14px; font-family:微软雅黑; margin:auto; } .tb{ border:solid 3px gray; margin-top:3px;} .r1{ background-color:Green;} .t1{ font-weight:bold;} .t2{ text-align:right; font-style:italic; color:Blue;} .t3{ color:#aabbcc; } .reply{ width:90%; border-bottom:dashed 1px green; height:20px; color:Purple;} </style> </head> <body> <form id="form1" runat="server"> <div id="main"> <h2>QQZone--xx的QQ空间</h2> <hr /> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <table class="tb" width="100%"> <tr class="r1"> <td height="30" width="50%" class="t1"> <%# Eval("Content") %> <%# Eval("postdate") %> <%# Eval("ip") %> </td> <td width="50%" class="t2"> <%# Container.ItemIndex+1 %>楼 </td> </tr> <tr> <td height="120" class="t3"> <%#Eval("IContent") %> <hr /> <asp:HiddenField ID="HiddenField1" Value='<%# Eval("lid") %>' runat="server" /> <asp:Repeater ID="Repeater2" runat="server"> <ItemTemplate> <div class="reply"> <%#Eval("postdate") %> <%# Eval("username") %> <%#Eval("rcontent") %> </ItemTemplate> </asp:Repeater> <br /> 姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 回复内容<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="提交" OnClick="addRelay" /> </td> </tr> </table> </ItemTemplate> </asp:Repeater> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; namespace WebApplication1 { public partial class QQzone : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindList(); } } //提交 protected void addRelay(object sender, EventArgs e) { //获取数据 Button btn= sender as Button; RepeaterItem pi= btn.Parent as RepeaterItem; string name = (pi.FindControl("TextBox1") as TextBox).Text; string lid = (pi.FindControl("HiddenField1") as HiddenField).Value; string rcontent = (pi.FindControl("TextBox2") as TextBox).Text; DateTime dt = DateTime.Now; string ip = Request.UserHostAddress; //数据新增操作 string q = "insert into leaveMsg values(@a,@b)"; SqlParameter[] pm=new SqlParameter[2]; pm[0]=new SqlParameter("@a",name); pm[1]=new SqlParameter("@b",rcontent); SqlDataReader dr = SQLHelper.GetDataReader(q,pm); BindList(); //新增后重新绑定 } private void BindList() { //绑定留言消息 string sql = "select * from leaveMsg"; DataTable dt = SQLHelper.GetTable(sql); Repeater1.DataSource = dt; Repeater1.DataBind(); //绑定回复消息 //1.遍历Repeater1的每一行 foreach (RepeaterItem item in Repeater1.Items) { //2.取这一行的隐藏域 HiddenField1,取值(留言id) HiddenField hf= item.FindControl("HiddenField1") as HiddenField; string id = hf.Value; //3.查询数据库中留言id对应的回复内容 string s = "select * from replyMsg where lid=@a"; SqlParameter pm = new SqlParameter("@a",id); DataTable d2 = SQLHelper.GetTable(s,pm); //4.把查询回复放到这一行的Repeater2上去 Repeater rp2 = item.FindControl("Repeater2")as Repeater; rp2.DataSource = d2; rp2.DataBind(); } } } }