分别用DataGrid、Repeater、DataList绑定XML数据的例子
2024-09-05 20:55:43
供稿:网友
网站运营seo文章大全提供全面的站长运营经验及seo技术!data.aspx
<%@ import namespace="system" %>
<%@ import namespace="system.io" %>
<%@ import namespace="system.xml" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.ado" %>
<script language="vb" runat=server>
dim ds as dataset = new dataset()
sub page_load(sender as object, e as eventargs)
dim fs as filestream
dim reader as streamreader
dim path as string
path = server.mappath( "books.xml" )
fs = new filestream(path, filemode.open, fileaccess.read)
reader = new streamreader(fs, encoding.default)
ds.readxml(reader)
grid1.datasource = ds.tables("book").defaultview
grid1.databind()
repeater1.datasource = ds.tables("book").defaultview
repeater1.databind()
datalist1.datasource = ds.tables("book").defaultview
datalist1.databind()
end sub
sub changepage(sender as object, e as datagridpagechangedeventargs)
grid1.datasource = ds.tables("book").defaultview
grid1.databind()
repeater1.datasource = ds.tables("book").defaultview
repeater1.databind()
datalist1.datasource = ds.tables("book").defaultview
datalist1.databind()
end sub
sub datalist_itemcommand(sender as object, e as datalistcommandeventargs)
select case e.commandsource.text
case "详细"
datalist1.selectedindex = e.item.itemindex
case "关闭"
datalist1.selectedindex = -1
end select
datalist1.datasource = ds.tables("book").defaultview
datalist1.databind()
end sub
</script>
<html>
<head>
</head>
<body style="background-color:f6e4c6">
<form runat="server">
<p>datagrid演示</p>
<asp:datagrid
allowpaging="true"
pagesize="10"
onpageindexchanged="changepage"
pagerstyle-horizontalalign="right"
pagerstyle-nextpagetext="下一頁"
pagerstyle-prevpagetext="上一頁"
headerstyle-backcolor="#aaaadd"
alternatingitemstyle-backcolor="#ffffc0"
bordercolor="black"
cellpadding="2"
cellspacing="0"
id="grid1" runat="server"/>
<p>repeater演示</p>
<table border="1">
<asp:repeater id="repeater1" runat="server">
<template name="headertemplate" >
<tr align="center"><th >书名</th><th>作者</th><th>价格</th></tr>
</template>
<template name="itemtemplate">
<tr><td><%# container.dataitem("title") %></td>
<td><%# container.dataitem("last-name") %> <%# container.dataitem("first-name") %></td>
<td><%# container.dataitem("price") %></td>
</tr>
</template>
</asp:repeater>
</table>
<p>datalist 演示</p>
<asp:datalist id="datalist1" runat="server"
border="1" bordercolor="black"
cellpadding="2" cellspacing="0"
headerstyle-backcolor="#888888"
itemstyle-backcolor="#eeeeee"
selecteditemstyle-backcolor="#ffffff"
headertemplate-colspan="3"
onitemcommand="datalist_itemcommand" >
<template name="headertemplate" >
</template>
<!--内容模版-->
<template name="itemtemplate">
书名:<%# container.dataitem("title") %>
<asp:linkbutton id="detail" runat="server" text="详细" forecolor="#333333"/>
</template>
<template name="selecteditemtemplate">
书名:<%# container.dataitem("title") %><br>
作者:<%# container.dataitem("last-name") %> <%# container.dataitem("first-name") %><br>
价格:<%# container.dataitem("price") %><br>
<div align="right"><asp:linkbutton id="title" runat="server" text="关闭" forecolor="#333333"/></div>
</template>
</asp:datalist>
</form>
</body>
</html>
books.xml
<?xml version="1.0" encoding="gb2312"?>
<newdataset>
<xsd:schema id="newdataset" targetnamespace="" xmlns="" xmlns:xsd="http://www.w3.org/1999/xmlschema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="book">
<xsd:complextype content="elementonly">
<xsd:all>
<xsd:element name="title" minoccurs="0" type="xsd:string"/>
<xsd:element name="first-name" minoccurs="0" type="xsd:string"/>
<xsd:element name="last-name" minoccurs="0" type="xsd:string"/>
<xsd:element name="price" minoccurs="0" type="xsd:float"/>
</xsd:all>
</xsd:complextype>
</xsd:element>
<xsd:element name="newdataset" msdata:isdataset="true">
<xsd:complextype>
<xsd:choice maxoccurs="unbounded">
<xsd:element ref="book"/>
</xsd:choice>
</xsd:complextype>
</xsd:element>
</xsd:schema>
<!-- this file represents a fragment of a book store inventory database -->
<bookstore>
<book genre="autobiography" publicationdate="1981" isbn="1-861003-11-0">
<title>the autobiography of benjamin franklin</title>
<author>
<first-name>benjamin</first-name>
<last-name>franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" isbn="0-201-63361-2">
<title>the confidence man</title>
<author>
<first-name>herman</first-name>
<last-name>melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" isbn="1-861001-57-6">
<title>the gorgias</title>
<author>
<name>plato</name>
</author>
<price>9.99</price>
</book>
<book genre="autobiography" publicationdate="1981" isbn="1-861003-11-0">
<title>the autobiography of benjamin franklin</title>
<author>
<first-name>benjamin</first-name>
<last-name>franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="autobiography" publicationdate="1981" isbn="1-861003-11-0">
<title>the autobiography of benjamin franklin</title>
<author>
<first-name>benjamin</first-name>
<last-name>franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="autobiography" publicationdate="1981" isbn="1-861003-11-0">
<title>the autobiography of benjamin franklin</title>
<author>
<first-name>benjamin</first-name>
<last-name>franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="autobiography" publicationdate="1981" isbn="1-861003-11-0">
<title>the autobiography of benjamin franklin</title>
<author>
<first-name>benjamin</first-name>
<last-name>franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="autobiography" publicationdate="1981" isbn="1-861003-11-0">
<title>the autobiography of benjamin franklin</title>
<author>
<first-name>benjamin</first-name>
<last-name>franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="autobiography" publicationdate="1981" isbn="1-861003-11-0">
<title>the autobiography of benjamin franklin</title>
<author>
<first-name>benjamin</first-name>
<last-name>franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="autobiography" publicationdate="1981" isbn="1-861003-11-0">
<title>the autobiography of benjamin franklin</title>
<author>
<first-name>benjamin</first-name>
<last-name>franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="autobiography" publicationdate="1981" isbn="1-861003-11-0">
<title>the autobiography of benjamin franklin</title>
<author>
<first-name>benjamin</first-name>
<last-name>franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="autobiography" publicationdate="1981" isbn="1-861003-11-0">
<title>the autobiography of benjamin franklin</title>
<author>
<first-name>benjamin</first-name>
<last-name>franklin</last-name>
</author>
<price>8.99</price>
</book>
</bookstore>
</newdataset>