以前完ajax都是用控制項糊裡糊塗的用,也沒有看他的原理, ajax無重新整理,就是通過javascript 調用另一個頁面,然後在另一個頁面重新整理,然後擷取資料
原理就是這樣簡單,代碼更簡單,,
這個demo,原理很簡單,就是首先擷取 getHttpObject的物件模型,然後去post,WebForm1.aspx,然後還回結果,然後解析!
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AjaxWebApp._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>無標題頁</title>
<script language="javascript">
function getHttpObject()
{
var waystation = null;
if(window.ActiveXObject)
{
waystation = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
waystation = new XMLHttpRequest();
}
else
{
waystation = false;
}
return waystation;
}
function addTest()
{
inputField = document.getElementById("textfield");
completeTable = document.getElementById("table1");
completeDiv = document.getElementById("popup");
completeBody = document.getElementById("body1");
var s = document.all.item("textfield").value;
// alert(s);
var aa= encodeURI(s);
if(s=="")
return;
var xmlrequest =getHttpObject();
xmlrequest.open("post","WebForm1.aspx?id="+aa);
xmlrequest.onreadystatechange = function()
{
if(xmlrequest.readyState==4)
{
//xmlrequest.responseText為你AddCity中輸出的那段字串;
setNames(xmlrequest.responseText);
}
}
xmlrequest.send(null);
}
function setNames(names)
{
if(names=="")
{
clearNames();
return ;
}
clearNames();
setOffsets();
var row,cell,txtNode;
var s = names.split("$");
for (var i = 0; i < s.length; i++)
{
//s[i]為td的
var nextNode =s[i];
row = document.createElement("tr");
cell = document.createElement("td");
cell.onmouseout = function() {this.style.backgroundColor='';};
cell.onmouseover = function() {this.style.backgroundColor='#ffff00';};
cell.onclick = function() { completeField(this); } ;
cell.pop="T";
txtNode = document.createTextNode(nextNode);
cell.appendChild(txtNode);
row.appendChild(cell);
document.getElementById("body1").appendChild(row);
}
}
//用來設定當滑鼠失去焦點後文字框的隱藏
document.onmousedown=function()
{
if(!event.srcElement.pop)
clearNames();
}//填寫輸入框
function completeField(cell)
{
inputField.value = cell.firstChild.nodeValue;
clearNames();
}
function clearNames()
{
completeBody = document.getElementById("body1");
var ind = completeBody.childNodes.length;
for(var i= ind-1;i>=0;i--)
{
completeBody.removeChild(completeBody.childNodes[i]);
}
completeDiv= document.getElementById("popup");
completeDiv.style.border = "none";
}
function setOffsets()
{
completeTable.style.width = inputField.offsetWidth;+"px";
var left = calculateOffset(inputField,"offsetLeft");
var top = calculateOffset(inputField,"offsetTop")+inputField.offsetHeight;
completeDiv.style.border = "black 1px solid";
completeDiv.style.left = left + "px";
completeDiv.style.top = top + "px";
}
function calculateOffset(field, attr) {
var offset = 0;
while(field) {
offset += field[attr];
field = field.offsetParent;
}
return offset;
}
</script>
</head>
<body>
<input name="textfield" id="textfield" type="text" class="input1" onkeyup="addTest();" maxlength="20" style="width: 68px" /><div id="popup" style="POSITION: absolute">
<table id="table1" cellspacing="0" cellpadding="0" bgcolor="#fffafa" border="0" >
<tbody id="body1" >
</tbody>
</table>
</div>
</body>
</html>
WebForm1.aspx 是用來後台重新整理的,所以
不需要前台頁面,可以把前台頁面的html去掉
:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AjaxWebApp.WebForm1" %>
WebForm1.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace AjaxWebApp
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string result = Request.QueryString["id"].ToString();
//可以在這裡面進行資料庫的操作,然後把得到的結果作為字串返回過去,然後再去解析那段字串;主意看看這個頁面的HTML
Response.Write("A$AA$AAA$AAAA");
//測試demo所以比較簡單,重新整理在這個頁面,所以你可以在這個頁面做任何事情
}
}
}
Demo不是最好,只是說明一些問題.../