ajax无翻页刷新简单实例2
2011-08-30 17:13
627 查看
1,HTML页面:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>微博样式1</title>
</head>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function addNumber()
{
createXMLHttpRequest();
//var url= "AjaxMicrobloggingPaging.aspx?Num1="+document.getElementById("num1").value+"&Num2="+document.getElementById("num2").value;
var url= "Handler.ashx";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=showResult;
xmlHttp.send(null);
}
function showResult()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
document.getElementById("mlist").innerHTML=xmlHttp.responseText;
}
}
}
</script>
<body>
<div class="content" id="mlist"></div>
<input type="button" name="fdfds" value="计算" onclick="addNumber();" />
</body>
</html>
2,动态页面
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Text;
using System.Data;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write(server_Side_Processing(5, 1));
}
public bool IsReusable {
get {
return false;
}
}
/// <summary>
///服务器端处理程序到数据库查询数据并生成xml档返回
/// </summary>
public string server_Side_Processing(int pageSize, int currentPage)
{
StringBuilder resultXML = new StringBuilder();
string str_xml;
DataSet ds;
DataAccess da;
int i;
resultXML.Append("<?xml version='1.0' encoding='gb2312'?>");
resultXML.Append("<ajax-response>\n");
resultXML.Append("<root>\n");
try
{
if (currentPage == 1)
{
str_xml = "select top " + pageSize + " * from tbMicroblog order by MicroblogID desc";
}
else
{
str_xml = "select top " + pageSize + " * from tbMicroblog MicroblogID not in (select top " + pageSize * (currentPage - 1) + " * from tbMicroblog order by MicroblogID desc) where order by MicroblogID desc";
}
da = new DataAccess();
ds = da.GetDataSetResult(str_xml, "xml_table");
if (ds != null)
{
for (i = 0; i < pageSize; i++)
{
if (ds.Tables[0].Rows[i] == null)
{
break;
}
resultXML.Append("<data>\n");
resultXML.Append("\t<MicroblogID>" + ds.Tables[0].Rows[i]["MicroblogID"].ToString() + "</MicroblogID>\n");
if (ds.Tables[0].Rows[i]["UserID"].ToString().Trim() != "")
{
resultXML.Append("\t<UserID>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["UserID"].ToString().Trim()) + "</UserID>\n");
}
else
{
resultXML.Append("\t<UserID>#</UserID>\n");
}
if (ds.Tables[0].Rows[i]["Content"].ToString().Trim() != "")
{
resultXML.Append("\t<Content>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["Content"].ToString().Trim()) + "</Content>\n");
}
else
{
resultXML.Append("\t<Content>#</Content>\n");
}
if (ds.Tables[0].Rows[i]["Pubdate"].ToString().Trim() != "")
{
resultXML.Append("\t<Pubdate>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["Pubdate"].ToString().Trim()) + "</Pubdate>\n");
}
else
{
resultXML.Append("\t<Pubdate>#</Pubdate>\n");
}
resultXML.Append("</data>\n");
}
}
else
{
resultXML.Append("<data>\n");
resultXML.Append("\t<nodata>" + "No Data !" + "</nodata>\n");
resultXML.Append("</data>\n");
}
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
resultXML.Append("</root>\n");
resultXML.Append("</ajax-response>");
return resultXML.ToString();
}
}
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>微博样式1</title>
</head>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function addNumber()
{
createXMLHttpRequest();
//var url= "AjaxMicrobloggingPaging.aspx?Num1="+document.getElementById("num1").value+"&Num2="+document.getElementById("num2").value;
var url= "Handler.ashx";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=showResult;
xmlHttp.send(null);
}
function showResult()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
document.getElementById("mlist").innerHTML=xmlHttp.responseText;
}
}
}
</script>
<body>
<div class="content" id="mlist"></div>
<input type="button" name="fdfds" value="计算" onclick="addNumber();" />
</body>
</html>
2,动态页面
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Text;
using System.Data;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write(server_Side_Processing(5, 1));
}
public bool IsReusable {
get {
return false;
}
}
/// <summary>
///服务器端处理程序到数据库查询数据并生成xml档返回
/// </summary>
public string server_Side_Processing(int pageSize, int currentPage)
{
StringBuilder resultXML = new StringBuilder();
string str_xml;
DataSet ds;
DataAccess da;
int i;
resultXML.Append("<?xml version='1.0' encoding='gb2312'?>");
resultXML.Append("<ajax-response>\n");
resultXML.Append("<root>\n");
try
{
if (currentPage == 1)
{
str_xml = "select top " + pageSize + " * from tbMicroblog order by MicroblogID desc";
}
else
{
str_xml = "select top " + pageSize + " * from tbMicroblog MicroblogID not in (select top " + pageSize * (currentPage - 1) + " * from tbMicroblog order by MicroblogID desc) where order by MicroblogID desc";
}
da = new DataAccess();
ds = da.GetDataSetResult(str_xml, "xml_table");
if (ds != null)
{
for (i = 0; i < pageSize; i++)
{
if (ds.Tables[0].Rows[i] == null)
{
break;
}
resultXML.Append("<data>\n");
resultXML.Append("\t<MicroblogID>" + ds.Tables[0].Rows[i]["MicroblogID"].ToString() + "</MicroblogID>\n");
if (ds.Tables[0].Rows[i]["UserID"].ToString().Trim() != "")
{
resultXML.Append("\t<UserID>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["UserID"].ToString().Trim()) + "</UserID>\n");
}
else
{
resultXML.Append("\t<UserID>#</UserID>\n");
}
if (ds.Tables[0].Rows[i]["Content"].ToString().Trim() != "")
{
resultXML.Append("\t<Content>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["Content"].ToString().Trim()) + "</Content>\n");
}
else
{
resultXML.Append("\t<Content>#</Content>\n");
}
if (ds.Tables[0].Rows[i]["Pubdate"].ToString().Trim() != "")
{
resultXML.Append("\t<Pubdate>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["Pubdate"].ToString().Trim()) + "</Pubdate>\n");
}
else
{
resultXML.Append("\t<Pubdate>#</Pubdate>\n");
}
resultXML.Append("</data>\n");
}
}
else
{
resultXML.Append("<data>\n");
resultXML.Append("\t<nodata>" + "No Data !" + "</nodata>\n");
resultXML.Append("</data>\n");
}
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
resultXML.Append("</root>\n");
resultXML.Append("</ajax-response>");
return resultXML.ToString();
}
}
相关文章推荐
- ajax无翻页刷新简单实例2
- ajax无翻页刷新简单实例
- ajax无翻页刷新简单实例2
- Ajax简单实例----无刷新求和运算
- 利用ajax实现简单的注册验证局部刷新实例
- 无刷新提交表单(php+js+ajax简单实例 )
- Ajax简单实例----无刷新求和运算
- 一个简单的ajax无刷新翻页的程序
- JQuery的Ajax请求实现局部刷新的简单实例
- jquery+ajax+C#实现无刷新操作数据库数据的简单实例
- php+ajax实时刷新简单实例
- 利用ajax实现简单的注册验证局部刷新实例
- 将form表单通过ajax实现无刷新提交的简单实例
- php+ajax实时刷新简单实例
- JQuery的Ajax请求实现局部刷新的简单实例
- 无刷新提交表单(php+jquery+ajax+json的一个最简单实例 )
- jquery+ajax+C#实现无刷新操作数据库数据的简单实例
- Ajax无刷新无加载服务器控件翻页实例
- strtuts+ajax 用户名无刷新验证简单实例
- Flash & Ajax 操作 XML 实例:无刷新分页