XML+XSLT+CSS+JQuery+WebService组建Asp.Net网站(3)
输入以下代码:
<%@ WebHandler Language="C#" Class="Default" %>
using System;
using System.Web;
using System.Data.OleDb;
using System.Data;
using System.Xml;
public class Default : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
OleDbConnection c = new OleDbConnection(string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""{0}"";Persist Security Info=True", HttpContext.Current.Server.MapPath(@"~\App_Data\Database.mdb")));
var ds = new DataSet();
c.Open();
//获取Url中的max参数
var max = 0;
Int32.TryParse(HttpContext.Current.Request.QueryString["max"],out max);
//读取数据
new OleDbDataAdapter(string.Format("select{0} * from [User]", max > 0 ? " top " + max : string.Empty), c).Fill(ds);
c.Close();
//建立XML文档
var xml = new XmlDocument();
xml.LoadXml(ds.GetXml());
//添加文档声明
xml.InsertBefore(xml.CreateXmlDeclaration("1.0", "UTF-8", null), xml.DocumentElement);
//添加xslt声明
var xsl = xml.CreateProcessingInstruction("xml-stylesheet", @"type=""text/xsl"" href=""Default.xslt""");
xml.InsertBefore(xsl, xml.DocumentElement);
//输出
context.Response.ContentType = "application/xml";
context.Response.Write(xml.InnerXml);
}
public bool IsReusable
{
get
{
return false;
}
}
}
再添加一个XSLT文件,命名为Default.xslt,输入以下代码:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:template match="/">
<html>
<hread>
<title>XML网站测试</title>
<link href="Style.css" rel="stylesheet" type="text/css"/>
<script src="jquery-1.3.2.js" type="text/javascript"/>
<script src="Post.js" type="text/javascript"/>
</hread>
<body>
<h1>XML网站测试</h1>
<ul>
<xsl:for-each select="//Table">
<li>
<a href="#">
<xsl:value-of select="ID"/>
</a>
<xsl:value-of select="UserName"/>
</li>
</xsl:for-each>
</ul>
<input type="text" name="Name" id="Name" />
<input type="submit" name="PostName" id="PostName" value="提交" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
注意,在XSLT中可包含用于提交数据的交互控件,但不需要为其定义事件处理,交互处理都由JS完成。
(责任编辑:IT教学网)