//Comment.htm
无刷新评论type="text/javascript">
//加载评论
$(function() {
$.post("GetComment.ashx",
function(data, status) {
if (status != "success") {
$("#ulComment").append($("
加载数据失败"));return;
}
var lines =
data.split("$");
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
var part = line.split("|");
var comment = $("
/>
用户ip:"+ part[0] + "
/>评论内容:" + part[1] +
"
/>时间:" + part[2] +
"
");$("#ulComment").append(comment);
}
})
})
//评论
$(function() {
$("#btnPost").click(function()
{
var txtComment =
$("#txtComment").val();
$.post("SetComment.ashx", {
"comment": txtComment },
function(data, status) {
if (status != "success") {
alert("评论失败,请重新评论");
return;
}
var lines =
data.split("|");
if (lines[0] == "ok") {
alert("谢谢评论");
var Comment = $("
/>
用户ip:"+ lines[1] + "
/>评论内容:" + txtComment +
"
/>时间:" + lines[2] +
"
");$("#ulComment").append(Comment);
$("#txtComment").val("");//清除文本框内信息
}
else {
alert("数据错误");
}
})
})
})
style="width: 662px"
>
style="list-style-type:none">北京梁蓓委员谈80后无力应对高房价的建议:我觉得80后男孩子如果买不起房子,80后女孩子可以嫁给
40岁的男人。80后的男人如果有条件了,到40岁再娶20岁的女孩子也是不错的选择。
id="txtComment"
cols="40"
rows="4">
/>
type="button"
id="btnPost"
value="提交评论"
/>
//SetComment.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ajax无刷新评论.DS_CommentTableAdapters;
namespace ajax无刷新评论
{
///
///
评论导入到数据库,并返回数据
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class
SetComment : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType =
"text/plain";
string comment =
context.Request["comment"];
new
T_CommentTableAdapter().InsertQuery(context.Request.UserHostAddress,comment,DateTime.Now);
context.Response.Write("ok|"+context.Request.UserHostAddress+"|"+DateTime.Now);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
//GetComment.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ajax无刷新评论.DS_CommentTableAdapters;
using System.Text;
namespace ajax无刷新评论
{
///
///
数据库中的数据送到页面
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class
GetComment : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType =
"text/plain";
var comments = new T_CommentTableAdapter().GetData();
StringBuilder str = new StringBuilder();//字符串,很给力的字符串
foreach (var comment in comments)
{
str.Append(comment.IP).Append("|").Append(comment.Comment).Append("|").Append(comment.DateTime).Append("$");
//用"|"隔开每个字段,用"$"隔开每一行
}
context.Response.Write(str.ToString().Trim('$'));//把最后面的$去掉
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
数据库结构:
DataSet结构:
演示截图: