asp.net Ajax之无刷新评论介绍

318人浏览 / 0人评论
首先还是建一个DoComments.aspx页面和一个DealComments.ashx页面(代码基本上都有注释,如果没写注释,请先看前几篇!)。
Docomments.aspx页面中的代码为:
复制代码 代码如下:




var objXmlHttp = null;
function CreateXMLHTTP() {
if (window.ActiveXObject) {
objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
if (window.XMLHttpRequest) {
objXmlHttp = new XMLHttpRequest();
} else {
alert("初始化XMLHTTP错误!");
}
}
}
function DoComments() {
var data = "txtComments" + document.getElementById("txtComment").value;
CreateXMLHTTP();
objXmlHttp.open("POST", "DealComments.ashx", true);
objXmlHttp.onreadystatechange = function () {//在服务器响应后调用
if (objXmlHttp.readyState >= 4) {
if (objXmlHttp.status == 200) {
var result = objXmlHttp.responseText;//获得服务器返回的字符串
if (result == "true") {
var cTable = document.getElementById("commentTable");//获得评论的表格对象
var newRow = cTable.insertRow(cTable.rows.length);//在表格的最后一行再添加一行
var cTd = newRow.insertCell();//在新添加的行中再添加一列
cTd.innerHTML = document.getElementById("txtComment").value;//设置列内容为刚发布的评论内容
} else {
alert("objXmlHttp.status");
}
}
}
}
objXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //添加自定义HTTP头道请求
objXmlHttp.send(data);//发送请求到服务器
}



评论信息:
cellpadding="0" cellspacing="0">

<%----%>


用户名
内容




cellpadding="0" cellspacing="0">










发布内容:








DealComments.ashx中的代码如下:
复制代码 代码如下:

public void ProcessRequest(HttpContext context)
{
string strComment = context.Request.Form["txtComments"];//获得传过来的内容
if (string.IsNullOrEmpty(strComment))//如果不为空,返回ture
{
context.Response.Write("true");
}
else
{
context.Response.Write("false");
}
context.Response.End();
}

简单吧!初学的童鞋......呵呵、、、、、、本系列只适合初学者,请大牛勿笑啊!

全部评论

晴天下起了小雨
2017-10-01 18:00
很喜欢,果断关注了
wjmyly7336064
2017-10-01 18:00
相当实用,赞美了
橘大佬
2017-10-01 18:00
就是有些细节再到位点就好了…