首先还是建一个DoComments.aspx页面和一个DealComments.ashx页面(代码基本上都有注释,如果没写注释,请先看前几篇!)。
<head runat="server">
<t
ITle></title>
<script ty
PE="text/javascript">
VAR obj
XMlHttp = null;
function CreateX
MLHTTP() {
if (window.ActiveXObject) {
objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
if (window.XMLHtt
PRequest) {
objXmlHttp = new XMLHttpRequest();
} else {
alert("初始化XMLHTTP错误!");
}
}
}
function DoComments() {
var data = "txtComments" + document.getElementById("txtComment").value;
CreateXMLHTTP();
objXmlHttp.open("POST", "DealComments.ashx", true);
objXmlHttp.onready
statechange = 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-url
encoded"); //添加自定义HTTP头道请求
objXmlHttp.send(data);//发送请求到服务器
}
</script>
</head>
<body>
评论信息:
<table id="commentTable" style="width: 600px; border: 1px solid
#000;" border="1"
cellpadding="0" cellspacing="0">
<tr>
<%--<td width="30%" class="style1">用户名</td>--%>
<td>
内容
</td>
</tr>
</table>
<
br />
<
hr />
<table style="width: 700px; border: 1px solid #000; text-align: left;" border="1"
cellpadding="0" cellspacing="0">
<tr>
<td>
发布内容:
</td>
</tr>
<tr>
<td>
<textarea id="txtComment" cols="60" rows="10"></textarea>
</td>
</tr>
<tr>
<td>
<input type="button" onclick="DoComments()" id="
BTnComment" value="发布评论" />
</td>
</tr>
</table>
</body>
</html>
p
ublic 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();
}