首先還是建一個DoComments.aspx頁面和一個DealComments.ashx頁面(代碼基本上都有注釋,如果沒寫注釋,請先看前幾篇!)。
Docomments.aspx頁面中的代碼為:
復制代碼 代碼如下:
head runat="server">
title>/title>
script type="text/javascript">
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;//設置列內容為剛發(fā)布的評論內容
} else {
alert("objXmlHttp.status");
}
}
}
}
objXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //添加自定義HTTP頭道請求
objXmlHttp.send(data);//發(fā)送請求到服務器
}
/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>
發(fā)布內容:
/td>
/tr>
tr>
td>
textarea id="txtComment" cols="60" rows="10">/textarea>
/td>
/tr>
tr>
td>
input type="button" onclick="DoComments()" id="btnComment" value="發(fā)布評論" />
/td>
/tr>
/table>
/body>
/html>
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();
}
簡單吧!初學的童鞋......呵呵、、、、、、本系列只適合初學者,請大牛勿笑?。?
您可能感興趣的文章:- 基于jquery實現(xiàn)ajax無刷新評論
- Asp.net利用JQuery AJAX實現(xiàn)無刷新評論思路與代碼
- ASP+Ajax實現(xiàn)無刷新評論簡單例子
- PHP Ajax實現(xiàn)頁面無刷新發(fā)表評論
- PHP Ajax實現(xiàn)頁面無刷新發(fā)表評論
- 一個jsp+AJAX評論系統(tǒng)
- Ajax實現(xiàn)評論提交
- 來自chinaz的ajax獲取評論代碼
- Ajax實現(xiàn)評論中頂和踩功能的實例代碼
- ajax無刷新評論功能