您的位置:首页 > 其它

分别用DataGrid、Repeater、DataList绑定XML数据的例子

2005-12-29 13:23 375 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐