网站导航免费论文 原创论文 论文搜索 原创论文 网学软件 学术大家 资料中心 会员中心 问题解答 原创论文 大学论文导航 设计下载 最新论文 下载排行 原创论文 论文源代码
返回网学首页
网学联系
最新论文 推荐专题 热门论文 素材专题
当前位置: 网学 > 编程文档 > ASP.net > 正文

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

来源:http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 14/02/27

鉴于大家对ASP.net十分关注,我们编辑小组在此为大家搜集整理了“用DataGrid、Repeater、DataList绑定XML数据的例子”一文,供大家参考学习

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>
  • 上一篇资讯: BigEagle的数据库结构
  • 网学推荐

    免费论文

    原创论文

    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号