对于XmlHttp的介绍见http://www.cnblogs.com/helimin19/archive/2004/12/03/72519.aspx
,
本文主要实现了在donet中,前台界面与后台程序完成分离,以达到互不影响的效果,从而降低了前台后台之前的偶合度。(当然这种效果也只是相对的),就果我下面举的例子来说吧,会员注册和修改会员资料是在一个页面上显示。注册可以达到完成分离(后台程序可以完全不知道前台界面是什么样子的,也不需要知道前面界面窗体中包括哪些内容,即不需要知道前台将要传些什么值给我),但修改会员数据,就需要前台传个用户名来过,从而显示指定的用户信息。如果你采用其Cookies等用户级的变量来传的话,当然就可以达到完成分离的效果了。
1。xmlHttp.js---用於處於客戶端的XMLHTTP數據
//將所有的客戶端表單數據生成XML文件
function GetAllFormData()
{
var strXML = "<Client>\r\n<FormData>\r\n";
for(var i=0;i<=document.forms(0).item.length;i++)
{
strXML += "<" + document.forms(0).item(i).name + ">";
strXML += document.forms(0).item(i).value;
strXML += "</" + document.forms(0).item(i).name + ">\r\n";
}
strXML += "</FormData>\r\n</Client>"
return strXML;
}
///向服務器發送XML文檔
function Send(Str,URL)
{
var Http = new ActiveXObject("Microsoft.XMLHTTP")
Http.open("POST",URL,false)
Http.send(Str)
return Http.responseText;
}
///獲得XML中指定的節點的值
function GetXMLNodeValue(strXML,nodeName)
{
var Dom = new ActiveXObject("Microsoft.XMLDOM")
Dom.async=false
Dom.loadXML(strXML)
if(Dom.parseError.errorCode != 0)
{
delete(Dom)
return(false)
}
else
{
var node = Dom.documentElement.selectSingleNode("//"+nodeName);
if(node)
nodeValue = node.text;
delete(Dom)
return(nodeValue);
}
}
function GetAllFormData()
{
var strXML = "<Client>\r\n<FormData>\r\n";
for(var i=0;i<=document.forms(0).item.length;i++)
{
strXML += "<" + document.forms(0).item(i).name + ">";
strXML += document.forms(0).item(i).value;
strXML += "</" + document.forms(0).item(i).name + ">\r\n";
}
strXML += "</FormData>\r\n</Client>"
return strXML;
}
///向服務器發送XML文檔
function Send(Str,URL)
{
var Http = new ActiveXObject("Microsoft.XMLHTTP")
Http.open("POST",URL,false)
Http.send(Str)
return Http.responseText;
}
///獲得XML中指定的節點的值
function GetXMLNodeValue(strXML,nodeName)
{
var Dom = new ActiveXObject("Microsoft.XMLDOM")
Dom.async=false
Dom.loadXML(strXML)
if(Dom.parseError.errorCode != 0)
{
delete(Dom)
return(false)
}
else
{
var node = Dom.documentElement.selectSingleNode("//"+nodeName);
if(node)
nodeValue = node.text;
delete(Dom)
return(nodeValue);
}
}
2. tesaspx.htm---客戶端文件(即注冊修改會員資料頁面)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>NewUser</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script language="javascript" src="xmlHttp.js"></script>
<script language=javascript>
<!--
///本頁的測試用例
function Test()
{
var tmp = Send(GetAllFormData(),"./test.aspx");
document.Form1.UserName.value = GetXMLNodeValue(tmp,"UserName");
document.Form1.PassWord.value = GetXMLNodeValue(tmp,"PassWord");
document.Form1.EMail.value = GetXMLNodeValue(tmp,"EMail");
document.Form1.Question.value = GetXMLNodeValue(tmp,"Question");
document.Form1.Answer.value = GetXMLNodeValue(tmp,"Answer");
}
//-->
</script>
</HEAD>
<body MS_POSITIONING="GridLayout" >
<form id="Form1" method="post" name="Form1">
<FONT face="新細明體">
<DIV style="Z-INDEX: 102; LEFT: 192px; WIDTH: 288px; POSITION: absolute; TOP: 64px; HEIGHT: 200px"
ms_positioning="FlowLayout">
<DIV>用戶名:<INPUT id="UserName" type="text" name="UserName"></DIV>
<DIV>密碼:<INPUT id="PassWord" type="password" name="PassWord" ></DIV>
<div id="HidPass" runat="server" ms_positioning="FlowLayout">
確認密碼:<INPUT id="ConPassWord" type="password" name="ConPassWord">
</div>
<DIV>EMail:<INPUT id="EMail" type="text" name="EMail"></DIV>
<DIV>密碼提示語:<INPUT id="Question" type="text" name="Question" ></DIV>
<DIV>密碼答案:<INPUT id="Answer" type="text" name="Answer" ></DIV>
<DIV id="HidSafeCode" runat="server">
驗證碼:<INPUT id="CheckCode" type="text" name="CheckCode" ><IMG src="SafeCode.aspx" border="0">
</DIV>
<INPUT type="reset" value="Reset" name="reset">
<INPUT id="Button3" type="button" value="XML" name="Button3" onclick="Test();" >
</DIV>
</FONT>
</form>
</body>
</HTML>
<HTML>
<HEAD>
<title>NewUser</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script language="javascript" src="xmlHttp.js"></script>
<script language=javascript>
<!--
///本頁的測試用例
function Test()
{
var tmp = Send(GetAllFormData(),"./test.aspx");
document.Form1.UserName.value = GetXMLNodeValue(tmp,"UserName");
document.Form1.PassWord.value = GetXMLNodeValue(tmp,"PassWord");
document.Form1.EMail.value = GetXMLNodeValue(tmp,"EMail");
document.Form1.Question.value = GetXMLNodeValue(tmp,"Question");
document.Form1.Answer.value = GetXMLNodeValue(tmp,"Answer");
}
//-->
</script>
</HEAD>
<body MS_POSITIONING="GridLayout" >
<form id="Form1" method="post" name="Form1">
<FONT face="新細明體">
<DIV style="Z-INDEX: 102; LEFT: 192px; WIDTH: 288px; POSITION: absolute; TOP: 64px; HEIGHT: 200px"
ms_positioning="FlowLayout">
<DIV>用戶名:<INPUT id="UserName" type="text" name="UserName"></DIV>
<DIV>密碼:<INPUT id="PassWord" type="password" name="PassWord" ></DIV>
<div id="HidPass" runat="server" ms_positioning="FlowLayout">
確認密碼:<INPUT id="ConPassWord" type="password" name="ConPassWord">
</div>
<DIV>EMail:<INPUT id="EMail" type="text" name="EMail"></DIV>
<DIV>密碼提示語:<INPUT id="Question" type="text" name="Question" ></DIV>
<DIV>密碼答案:<INPUT id="Answer" type="text" name="Answer" ></DIV>
<DIV id="HidSafeCode" runat="server">
驗證碼:<INPUT id="CheckCode" type="text" name="CheckCode" ><IMG src="SafeCode.aspx" border="0">
</DIV>
<INPUT type="reset" value="Reset" name="reset">
<INPUT id="Button3" type="button" value="XML" name="Button3" onclick="Test();" >
</DIV>
</FONT>
</form>
</body>
</HTML>
3.xmlHttp.cs---重載Page類(操作客戶端以XmlHttp發過的請求)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Collections.Specialized;
namespace Model.Code
{
/// <summary>
/// xmlHttp---對XmlHttp的操作(繼承自OverridePage類,表示其子類都是對XmlHttp發送過來的數據進行操作)
/// 冷風.net,2005/03/26
/// </summary>
public class XmlHttp : OverridePage
{
public XmlHttp(){}
/// <summary>
/// 生成XmlDocument對象
/// 作者:冷風,net,2005/03/26
/// </summary>
/// <param name="stream">xml格式的內存塊</param>
/// <returns>XmlDocument對象</returns>
protected System.Xml.XmlDocument CreateXmlDocument(System.IO.Stream stream)
{
System.Xml.XmlDocument doc = new XmlDocument();
try
{
doc.Load(stream);
}
catch
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer,0,buffer.Length);
string strXML = System.Text.UnicodeEncoding.Default.GetString(buffer,0,buffer.Length);
doc.LoadXml(strXML);
}
return doc;
}
/// <summary>
/// 獲得XmlNode中第一個指定的節點的文本值
/// 作者:冷風,net,2005/03/26
/// </summary>
/// <param name="node">XmlNode對象</param>
/// <param name="nodeName">要搜索的節點名稱</param>
/// <returns>節點的值</returns>
protected string GetXMLNodeValue(XmlNode node,string nodeName)
{
XmlNode subNode = node.SelectSingleNode("//"+nodeName);
return subNode.InnerText;
}
/// <summary>
/// 將XmlNode對象轉化為NameValueCollection對象
/// 作者:冷風,net,2005/03/26
/// </summary>
/// <param name="node">XmlNode對象</param>
/// <returns>NameValueCollection對象</returns>
protected NameValueCollection CreateNameValueCollectionFromXML(XmlNode node)
{
NameValueCollection nvc = new NameValueCollection();
for(int i=0;i<node.ChildNodes.Count;i++)
{
nvc.Add(node.ChildNodes[i].Name,node.ChildNodes[i].InnerText);
}
return nvc;
}
/// <summary>
/// 為了不返回不必要的數據,必須重載Render方法
/// 作者:冷風,net,2005/03/26
/// </summary>
/// <param name="writer">輸出參數</param>
protected override void Render(HtmlTextWriter writer)
{
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Collections.Specialized;
namespace Model.Code
{
/// <summary>
/// xmlHttp---對XmlHttp的操作(繼承自OverridePage類,表示其子類都是對XmlHttp發送過來的數據進行操作)
/// 冷風.net,2005/03/26
/// </summary>
public class XmlHttp : OverridePage
{
public XmlHttp(){}
/// <summary>
/// 生成XmlDocument對象
/// 作者:冷風,net,2005/03/26
/// </summary>
/// <param name="stream">xml格式的內存塊</param>
/// <returns>XmlDocument對象</returns>
protected System.Xml.XmlDocument CreateXmlDocument(System.IO.Stream stream)
{
System.Xml.XmlDocument doc = new XmlDocument();
try
{
doc.Load(stream);
}
catch
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer,0,buffer.Length);
string strXML = System.Text.UnicodeEncoding.Default.GetString(buffer,0,buffer.Length);
doc.LoadXml(strXML);
}
return doc;
}
/// <summary>
/// 獲得XmlNode中第一個指定的節點的文本值
/// 作者:冷風,net,2005/03/26
/// </summary>
/// <param name="node">XmlNode對象</param>
/// <param name="nodeName">要搜索的節點名稱</param>
/// <returns>節點的值</returns>
protected string GetXMLNodeValue(XmlNode node,string nodeName)
{
XmlNode subNode = node.SelectSingleNode("//"+nodeName);
return subNode.InnerText;
}
/// <summary>
/// 將XmlNode對象轉化為NameValueCollection對象
/// 作者:冷風,net,2005/03/26
/// </summary>
/// <param name="node">XmlNode對象</param>
/// <returns>NameValueCollection對象</returns>
protected NameValueCollection CreateNameValueCollectionFromXML(XmlNode node)
{
NameValueCollection nvc = new NameValueCollection();
for(int i=0;i<node.ChildNodes.Count;i++)
{
nvc.Add(node.ChildNodes[i].Name,node.ChildNodes[i].InnerText);
}
return nvc;
}
/// <summary>
/// 為了不返回不必要的數據,必須重載Render方法
/// 作者:冷風,net,2005/03/26
/// </summary>
/// <param name="writer">輸出參數</param>
protected override void Render(HtmlTextWriter writer)
{
}
}
}
4.test.aspx---對應的tesaspx.htm的請求文件
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Collections.Specialized;
namespace Model.VIP
{
/// <summary>
/// test 的摘要描述。
/// </summary>
public class test : Code.XmlHttp
{
private string m_strXml="";
private void Page_Load(object sender, System.EventArgs e)
{
XmlDocument doc = this.CreateXmlDocument(this.Request.InputStream);
string userName = GetXMLNodeValue(doc.DocumentElement,"UserName");
DataSet ds = Code.Users.GetUserInfo(userName);
//如果存在數值則表明是修改會員信息,否則為新增加
if(ds!=null && ds.Tables[0].Rows.Count>0)
{
m_strXml = ds.GetXml();
}
else
{
NameValueCollection userInfo = this.CreateNameValueCollectionFromXML(doc.DocumentElement.FirstChild);
string returnValue = Code.Users.AddUser(userInfo);
if(returnValue=="OK")
{
m_strXml ="注冊成功!";
}
else
{
m_strXml = "注冊失敗 !";
}
}
Response.Write(m_strXml);
}
Web Form 設計工具產生的程式碼
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Collections.Specialized;
namespace Model.VIP
{
/// <summary>
/// test 的摘要描述。
/// </summary>
public class test : Code.XmlHttp
{
private string m_strXml="";
private void Page_Load(object sender, System.EventArgs e)
{
XmlDocument doc = this.CreateXmlDocument(this.Request.InputStream);
string userName = GetXMLNodeValue(doc.DocumentElement,"UserName");
DataSet ds = Code.Users.GetUserInfo(userName);
//如果存在數值則表明是修改會員信息,否則為新增加
if(ds!=null && ds.Tables[0].Rows.Count>0)
{
m_strXml = ds.GetXml();
}
else
{
NameValueCollection userInfo = this.CreateNameValueCollectionFromXML(doc.DocumentElement.FirstChild);
string returnValue = Code.Users.AddUser(userInfo);
if(returnValue=="OK")
{
m_strXml ="注冊成功!";
}
else
{
m_strXml = "注冊失敗 !";
}
}
Response.Write(m_strXml);
}
Web Form 設計工具產生的程式碼
}
}
個人感覺這麼做,可以方便前台人台與後台人員合作開發時,前台人台可以完全不懂donet就行了,這樣對於前台的如何更改,是否采用該頁都不會影響後台的程式。