Introduction to asp.net Ajax without refreshing comments

Source: Internet
Author: User

First, create a DoComments. aspx page and a DealComments. ashx page (the Code basically has comments. If no comments are written, read the first few articles first !).
The Code on the Docomments. aspx page is: Copy codeThe Code is as follows: <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 initialization error! ");
}
}
}
Function DoComments (){
Var data = "txtComments" + document. getElementById ("txtComment"). value;
CreateXMLHTTP ();
ObjXmlHttp. open ("POST", "DealComments. ashx", true );
ObjXmlHttp. onreadystatechange = function () {// call after the server responds
If (objXmlHttp. readyState> = 4 ){
If (objXmlHttp. status = 200 ){
Var result = objXmlHttp. responseText; // obtain the string returned by the server
If (result = "true "){
Var cTable = document. getElementById ("commentTable"); // The table object for receiving comments
Var newRow = cTable. insertRow (cTable. rows. length); // Add a row to the last row of the table.
Var cTd = newRow. insertCell (); // Add another column to the newly added row
CTd. innerHTML = document. getElementById ("txtComment"). value; // you can specify the column content as the post content.
} Else {
Alert ("objXmlHttp. status ");
}
}
}
}
ObjXmlHttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); // Add a custom HTTP header request
ObjXmlHttp. send (data); // send the request to the server
}
</Script>
</Head>
<Body>
Comment:
<Table id = "commentTable" style = "width: 600px; border: 1px solid #000;" border = "1"
Cellpadding = "0" cellspacing = "0">
<Tr>
<% -- <Td width = "30%" class = "style1"> 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>
Release 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 = "publish comments"/>
</Td>
</Tr>
</Table>
</Body>
</Html>

The code in DealComments. ashx is as follows:Copy codeThe Code is as follows: public void ProcessRequest (HttpContext context)
{
String strComment = context. Request. Form ["txtComments"]; // obtain the uploaded content.
If (string. IsNullOrEmpty (strComment) // if not empty, true is returned.
{
Context. Response. Write ("true ");
}
Else
{
Context. Response. Write ("false ");
}
Context. Response. End ();
}

Easy! Beginner's shoes... haha, and this series is only suitable for beginners. Please do not laugh!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.