asp.net Ajax之無(wú)刷新評(píng)論介紹
更新時(shí)間:2012年11月07日 15:35:57 作者:
asp.net Ajax之無(wú)刷新評(píng)論介紹;需要的朋友可以參考下
首先還是建一個(gè)DoComments.aspx頁(yè)面和一個(gè)DealComments.ashx頁(yè)面(代碼基本上都有注釋?zhuān)绻麤](méi)寫(xiě)注釋?zhuān)?qǐng)先看前幾篇!)。
Docomments.aspx頁(yè)面中的代碼為:
<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錯(cuò)誤!");
}
}
}
function DoComments() {
var data = "txtComments" + document.getElementById("txtComment").value;
CreateXMLHTTP();
objXmlHttp.open("POST", "DealComments.ashx", true);
objXmlHttp.onreadystatechange = function () {//在服務(wù)器響應(yīng)后調(diào)用
if (objXmlHttp.readyState >= 4) {
if (objXmlHttp.status == 200) {
var result = objXmlHttp.responseText;//獲得服務(wù)器返回的字符串
if (result == "true") {
var cTable = document.getElementById("commentTable");//獲得評(píng)論的表格對(duì)象
var newRow = cTable.insertRow(cTable.rows.length);//在表格的最后一行再添加一行
var cTd = newRow.insertCell();//在新添加的行中再添加一列
cTd.innerHTML = document.getElementById("txtComment").value;//設(shè)置列內(nèi)容為剛發(fā)布的評(píng)論內(nèi)容
} else {
alert("objXmlHttp.status");
}
}
}
}
objXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //添加自定義HTTP頭道請(qǐng)求
objXmlHttp.send(data);//發(fā)送請(qǐng)求到服務(wù)器
}
</script>
</head>
<body>
評(píng)論信息:
<table id="commentTable" style="width: 600px; border: 1px solid #000;" border="1"
cellpadding="0" cellspacing="0">
<tr>
<%--<td width="30%" class="style1">用戶名</td>--%>
<td>
內(nèi)容
</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ā)布內(nèi)容:
</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ā)布評(píng)論" />
</td>
</tr>
</table>
</body>
</html>
DealComments.ashx中的代碼如下:
public void ProcessRequest(HttpContext context)
{
string strComment = context.Request.Form["txtComments"];//獲得傳過(guò)來(lái)的內(nèi)容
if (string.IsNullOrEmpty(strComment))//如果不為空,返回ture
{
context.Response.Write("true");
}
else
{
context.Response.Write("false");
}
context.Response.End();
}
簡(jiǎn)單吧!初學(xué)的童鞋......呵呵、、、、、、本系列只適合初學(xué)者,請(qǐng)大牛勿笑啊!
Docomments.aspx頁(yè)面中的代碼為:
復(fù)制代碼 代碼如下:
<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錯(cuò)誤!");
}
}
}
function DoComments() {
var data = "txtComments" + document.getElementById("txtComment").value;
CreateXMLHTTP();
objXmlHttp.open("POST", "DealComments.ashx", true);
objXmlHttp.onreadystatechange = function () {//在服務(wù)器響應(yīng)后調(diào)用
if (objXmlHttp.readyState >= 4) {
if (objXmlHttp.status == 200) {
var result = objXmlHttp.responseText;//獲得服務(wù)器返回的字符串
if (result == "true") {
var cTable = document.getElementById("commentTable");//獲得評(píng)論的表格對(duì)象
var newRow = cTable.insertRow(cTable.rows.length);//在表格的最后一行再添加一行
var cTd = newRow.insertCell();//在新添加的行中再添加一列
cTd.innerHTML = document.getElementById("txtComment").value;//設(shè)置列內(nèi)容為剛發(fā)布的評(píng)論內(nèi)容
} else {
alert("objXmlHttp.status");
}
}
}
}
objXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //添加自定義HTTP頭道請(qǐng)求
objXmlHttp.send(data);//發(fā)送請(qǐng)求到服務(wù)器
}
</script>
</head>
<body>
評(píng)論信息:
<table id="commentTable" style="width: 600px; border: 1px solid #000;" border="1"
cellpadding="0" cellspacing="0">
<tr>
<%--<td width="30%" class="style1">用戶名</td>--%>
<td>
內(nèi)容
</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ā)布內(nèi)容:
</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ā)布評(píng)論" />
</td>
</tr>
</table>
</body>
</html>
DealComments.ashx中的代碼如下:
復(fù)制代碼 代碼如下:
public void ProcessRequest(HttpContext context)
{
string strComment = context.Request.Form["txtComments"];//獲得傳過(guò)來(lái)的內(nèi)容
if (string.IsNullOrEmpty(strComment))//如果不為空,返回ture
{
context.Response.Write("true");
}
else
{
context.Response.Write("false");
}
context.Response.End();
}
簡(jiǎn)單吧!初學(xué)的童鞋......呵呵、、、、、、本系列只適合初學(xué)者,請(qǐng)大牛勿笑啊!
您可能感興趣的文章:
- 基于jquery實(shí)現(xiàn)ajax無(wú)刷新評(píng)論
- Asp.net利用JQuery AJAX實(shí)現(xiàn)無(wú)刷新評(píng)論思路與代碼
- ASP+Ajax實(shí)現(xiàn)無(wú)刷新評(píng)論簡(jiǎn)單例子
- PHP Ajax實(shí)現(xiàn)頁(yè)面無(wú)刷新發(fā)表評(píng)論
- PHP Ajax實(shí)現(xiàn)頁(yè)面無(wú)刷新發(fā)表評(píng)論
- 一個(gè)jsp+AJAX評(píng)論系統(tǒng)
- Ajax實(shí)現(xiàn)評(píng)論提交
- 來(lái)自chinaz的ajax獲取評(píng)論代碼
- Ajax實(shí)現(xiàn)評(píng)論中頂和踩功能的實(shí)例代碼
- ajax無(wú)刷新評(píng)論功能
相關(guān)文章
ASP.NET?Core中Startup類(lèi)、Configure()方法及中間件詳解
本文詳細(xì)講解了ASP.NET?Core中Startup類(lèi)、Configure()方法及中間件,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01.NET?6全新配置對(duì)象ConfigurationManager介紹
這篇文章介紹了.NET?6全新配置對(duì)象ConfigurationManager,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11在ASP.NET 中實(shí)現(xiàn)單點(diǎn)登錄
在ASP.NET 中實(shí)現(xiàn)單點(diǎn)登錄...2007-03-03.Net創(chuàng)建型設(shè)計(jì)模式之原型模式(Prototype)
這篇文章介紹了.Net設(shè)計(jì)模式之原型模式(Prototype),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05