欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在asp.net(C#)中采用自定义标签和XML、XSL显示数据

程序员文章站 2024-03-09 10:52:17
标签定义复制代码 代码如下:public class encoding { public string encode(string csource) { return sy...
标签定义
复制代码 代码如下:

public class encoding
{
public string encode(string csource)
{
return system.web.httputility.htmlencode(csource);
}
}
public class emlist : label
{
public override bool enableviewstate
{
get{ return false;}
}
public string xslfile{get;set;}
public object serialobject{get;set;}
protected override void render(htmltextwriter writer)
{
if (serialobject == null)
{
throw new exception("对象未初始化");
}
system.xml.serialization.xmlserializer oserial = new system.xml.serialization.xmlserializer(serialobject.gettype());
system.text.stringbuilder osb = new system.text.stringbuilder();
system.io.stringwriter owr = new system.io.stringwriter(osb);
string xml = "";
oserial.serialize(owr, serialobject);
xml =osb.tostring();
string cxslfilename = this.mappathsecure(xslfile);
if (!system.io.file.exists(cxslfilename))
{
throw new exception("请加自己的处理异常程序");
}
system.xml.xsl.xsltargumentlist xslargs = new system.xml.xsl.xsltargumentlist();
encoding oen = new encoding();
xslargs.addextensionobject("urn:encoding", oen);
system.xml.xmldocument odoc = new system.xml.xmldocument();
try
{
odoc.loadxml(xml);
}
catch
{
throw new exception("请加自己的处理异常程序");
}
system.xml.xsl.xslcompiledtransform otran = new system.xml.xsl.xslcompiledtransform();
string cxsl = "";
try
{
cxsl = system.io.file.readalltext(cxslfilename);
}
catch
{
throw new exception("请加自己的处理异常程序");
}
system.io.stringreader osr=new system.io.stringreader(cxsl);
system.xml.xmlreader ore=system.xml.xmlreader.create(osr);
try
{
otran.load(ore);
}
catch
{
throw new exception("请加自己的处理异常程序");
}
try
{
otran.transform(odoc, xslargs, writer);
}
catch
{
throw new exception("请加自己的处理异常程序");
}
}
}
public class pagebar : system.web.ui.htmlcontrols.htmlcontrol
{
public int pagenum{get;set;}
public int pagesize { get; set; }
public int pagecount { get; set; }
public string baseurl{get;set;}
protected override void render(htmltextwriter writer)
{
writer.write(string.format("<a href={0}?pagenum=1>第一页</a>|<a href={0}?pagenum={1}>上一页</a>|<a href={0}?pagenum={2}>下一页</a>|<a href={0}?pagenum={3}>尾页</a>  (共{4}当前页{5})", baseurl, pagenum - 1 > 0 ? pagenum - 1 : 1, pagenum + 1 > pagecount ? pagecount : pagenum + 1, pagecount, pagecount, pagenum));
}
}

页面定义
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="webapplication1._default" %>
<%@register tagprefix="cs" namespace="webapplication1.control" assembly=" webapplication1" %>
<!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>
</head>
<body>
<cs:emlist serialobject="<%#list%>" xslfile="xsl/test.xslt" runat="server" /><br />
<cs:pagebar pagenum="<%#pagenum%>" baseurl="<%#request.path%>" pagecount="5" runat="server" />
</body>
</html>

其中list和pagenum为页面属性

xslt:
复制代码 代码如下:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0" xmlns:encoding="urn:encoding">
<xsl:output method="text" />
<xsl:template match="/">
<h2>
<xsl:for-each select="arrayofemployeeentity/employeeentity">
<![cdata[<a href="//www.jb51.net/article/9720.htm]]><xsl:value-of select="encoding:encode(employeeid)"/><![cdata[" alt="]]><xsl:value-of select= "encoding:encode(full_name)"/><![cdata["><br/>]]><xsl:value-of select= "encoding:encode(email_address)"/></xsl:for-each>
</h2>
</xsl:template>
</xsl:stylesheet>