分别用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>
<%@ 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>
相关文章推荐
- 分别用DataGrid、Repeater、DataList绑定XML数据的例子
- ASP.Net提供了三个数据控件,;这三个控件属于数据Web控件,分别是DataGrid,DataList和Repeater。
- datagrid数据绑定并格式化的一个例子
- ASP.NET 2.0 的数据绑定控件概述与区别(GridView、DetailsView、FormView 、Repeater、DataList)
- 数据绑定技术(table,DataSet ,DataView,DataReader ,DropDownList,DataBinder.Eval(),datagrid,dr,)ArrayList,Hashtable绑定到ListBox控件,xml
- ASP.NET 2.0 的数据绑定控件概述与区别(GridView、DetailsView、FormView 、Repeater、DataList)
- 数据绑定控件收藏 GridView DataList Repeater FormView DetailsView
- DataRow[]绑定到datalist gatagrid repeater数据控件
- 数据绑定控件Repeater/GridView/DataList在前台显示正确的文本格式收藏
- DataList绑定xml数据,并实现删除和修改
- Control Study -> Asp.net几个较复杂的显示数据控件(Repeater/DataList/DataGrid)用法 (示例代码下载)
- flex2使用HTTPService得到xml数据绑定到datagrid
- Control Study -> Asp.net几个较复杂的显示数据控件(Repeater/DataList/DataGrid)用法 (示例代码下载)
- Control Study -> Asp.net几个较复杂数据控件(Repeater/DataList/DataGrid)用法 (示例代码下载)
- ASP.NET 2.0 的数据绑定控件概述与区别(GridView、DetailsView、FormView 、Repeater、DataList)
- ASP.NET 2.0 的数据绑定控件概述与区别(GridView、DetailsView、FormView 、Repeater、DataList)
- DataGrid,DataList,Repeater等数据访问控件的区别及总结、使用Repeater控件显示数据
- 如何用XML数据绑定前台Repeater
- 动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- 3.Repeater 绑定数据例子