前台代码,
<head runat="server">
<title></title>
<style type="text/css">
#divfa a{ font-size:20px; font-weight:bold; text-decoration:none}
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="width:500px">
<p>河北软件职业技术学院CSDN办公大楼,已经投入使用,电梯.......</p>
</div>
<div id="div2">
<br />
<asp:TextBox ID="TextBox1" runat="server" Height="93px" MaxLength="300"
TextMode="MultiLine" Width="500px"></asp:TextBox>
<br />
<span style="margin-left:380px">
<asp:Button ID="btnPingLun" Font-Bold="true" runat="server" Text="发表评论"
Width="115px" οnclick="btnPingLun_Click" /></span>
</div>
<div style="width:100px"><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
<div id="divshow" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" onpageindexchanging="GridView1_PageIndexChanging"
PageSize="8" UseAccessibleHeader="False" Width="500px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table class="style1">
<%--<tr>
<td colspan="2" style="background-color: #999999">
<%#Eval("UserName")%></td>
</tr>--%>
<tr>
<td colspan="2">
<%#Eval("ComContent")%>
</td>
</tr>
<tr>
<td>
<%#Eval("NowDate")%>
</td>
<td style="text-align: right">
<asp:Button ID="Button1" runat="server" BorderStyle="None" Style="color: #0000FF"
Text="分享" />
<asp:Button ID="Button2" runat="server" BorderStyle="None" Style="color: #0000FF"
Text="回复" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</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.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace 高仿优酷评论留言板
{
public partial class WebForm1 : System.Web.UI.Page
{
string sqlstr = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
GETRecordAll();
BindData();
}
}
public void GETRecordAll()
{
SqlConnection conn = new SqlConnection(sqlstr);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select count(*) from Content";
int a = Convert.ToInt32(cmd.ExecuteScalar());
this.Label1.Text = "全部评论"+"("+a+")";
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
private void BindData()
{
using(SqlConnection conn=new SqlConnection(sqlstr))
{
conn.Open();
using(SqlCommand cmd=conn.CreateCommand())
{
cmd.CommandText = "select Content.ComContent,NowDate from Content";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
}
protected void btnPingLun_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(sqlstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into Content(ComContent,NowDate)values(@content,@time)";
cmd.Parameters.AddWithValue("@content", this.TextBox1.Text.Trim());
cmd.Parameters.AddWithValue("@time", DateTime.Now);
int n = cmd.ExecuteNonQuery();
if (n > 0)
{
ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('评论成功!')</script>", false);
GETRecordAll();
BindData();
}
else
{
ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('评论失败!')</script>", false);
}
}
}
}
}
}
首先在数据库建立一张数据表,如下图:
下面就是效果图:在下面的文本框内输入你的评论,点击“发表评论”,则会弹出发表成功的提示!
同时,评论的人数发生改变。点击回复之后还可以继续评论。
这是基于上一张图片,我随便写了几个字,点击“发表评论”后的效果;