asp.net Ajax no refresh comment introduction
- 2020-05-17 05:22:04
- OfStack
The first step is to create an DoComments.aspx page and an DealComments.ashx page. .
The code in the Docomments.aspx page is:
The code in DealComments. ashx is as follows:
Simple!!!! Beginner's shoes... Hehe,,,,, this series is only suitable for beginners, please don't laugh!
The code in the Docomments.aspx page is:
<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(" Initialize the XMLHTTP Error! ");
}
}
}
function DoComments() {
var data = "txtComments" + document.getElementById("txtComment").value;
CreateXMLHTTP();
objXmlHttp.open("POST", "DealComments.ashx", true);
objXmlHttp.onreadystatechange = function () {// Called after the server responds
if (objXmlHttp.readyState >= 4) {
if (objXmlHttp.status == 200) {
var result = objXmlHttp.responseText;// Gets the string returned by the server
if (result == "true") {
var cTable = document.getElementById("commentTable");// Get the table object for the comment
var newRow = cTable.insertRow(cTable.rows.length);// It's at the end of the table 1 Line to add 1 line
var cTd = newRow.insertCell();// Add in the newly added line 1 column
cTd.innerHTML = document.getElementById("txtComment").value;// Set the column content to the comments that have just been posted
} else {
alert("objXmlHttp.status");
}
}
}
}
objXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // Add customization HTTP Head request
objXmlHttp.send(data);// Send the request to the server
}
</script>
</head>
<body>
Comment information:
<table id="commentTable" style="width: 600px; border: 1px solid #000;" border="1"
cellpadding="0" cellspacing="0">
<tr>
<%--<td width="30%" class="style1"> The user name </td>--%>
<td>
content
</td>
</tr>
</table>
<br />
<hr />
<table style="width: 700px; border: 1px solid #000; text-align: left;" border="1"
cellpadding="0" cellspacing="0">
<tr>
<td>
Published content:
</td>
</tr>
<tr>
<td>
<textarea id="txtComment" cols="60" rows="10"></textarea>
</td>
</tr>
<tr>
<td>
<input type="button" onclick="DoComments()" id="btnComment" value=" Post a comment " />
</td>
</tr>
</table>
</body>
</html>
The code in DealComments. ashx is as follows:
public void ProcessRequest(HttpContext context)
{
string strComment = context.Request.Form["txtComments"];// Get the content passed
if (string.IsNullOrEmpty(strComment))// If not null, returns ture
{
context.Response.Write("true");
}
else
{
context.Response.Write("false");
}
context.Response.End();
}
Simple!!!! Beginner's shoes... Hehe,,,,, this series is only suitable for beginners, please don't laugh!