ajax应用-留言本(asp.net)
ajax应用-留言本(asp.net),改自web 2.0开发技术祥解书中的asp例子,改为现在的asp.net版本,程序用vs用网站的形式打开 (vs2005或者vs 2008)。
下载源码:下载
AJAXbook.rar (14.2 KiB, 168 hits)
主要源码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Book_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ajax GuestBook</title>
<style type="text/css">
body { font-size:0.75em;text-align:center;}
dl { margin:0;}
dt { background-color:#666;color:#fff;margin:1px;padding:0 3px;}
dd { margin:3px;}
div { margin:auto;line-height:150%;text-align:left;width:400px;border:1px solid #666;}
#postBox { margin-top:10px;}
dd.button { text-align:center;}
dd.button input { margin:0 20px;}
</style>
<script type="text/javascript">
<!--
//将用户输入异步提交到服务器
function ajaxSubmit(){
//获取用户输入
var title=document.forms[0].title.value;
var author=document.forms[0].author.value;
var content=document.forms[0].content.value;
//创建XMLHttpRequest对象
var xmlhttp;
try{
xmlhttp=new XMLHttpRequest();
}catch(e){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//创建请求结果处理程序
xmlhttp.onreadystatechange=function(){
if (4==xmlhttp.readyState){
if (200==xmlhttp.status){
var date=xmlhttp.responseText;
addToList(date);
}else{
alert("error");
}
}
}
//打开连接,true表示异步提交
xmlhttp.open("post", "ajaxAdd.aspx", true);
//当方法为post时需要如下设置http头
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
//发送数据
xmlhttp.send("title="+escape(title)+"&author="+escape(author)+"&content="+escape(content));
}
//将用户输入显示到页面
function addToList(date){
//获取留言列表div容器
var msg=document.getElementById("msgList");
//创建dl标记及其子标记
var dl=document.createElement("dl");
var dt=document.createElement("dt");
var dd=document.createElement("dd");
var dd2=document.createElement("dd");
//将结点插入到相应的位置
msg.insertBefore(dl,msg.firstChild);
dl.appendChild(dt);
dl.appendChild(dd);
dl.appendChild(dd2);
//填充留言内容
dt.innerHTML="标题:"%2
原创文章转载请注明出处:云飞扬IT的blog