datalist控件,xml数据源,SqlDataSource数据源
例1:数据源为xml
Code
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:datalist id="DataList1" DataSourceID="XmlDataSource1" runat="server">
<ItemTemplate>
<p><b><%# XPath("author/first-name") %> <%# XPath("author/last-name")%></b> wrote <%# XPath("title") %></p>
</ItemTemplate>
</asp:datalist>
<asp:xmldatasource id="XmlDataSource1" runat="server" datafile="~/Books.xml" xpath="//bookstore/book"/>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:datalist id="DataList1" DataSourceID="XmlDataSource1" runat="server">
<ItemTemplate>
<p><b><%# XPath("author/first-name") %> <%# XPath("author/last-name")%></b> wrote <%# XPath("title") %></p>
</ItemTemplate>
</asp:datalist>
<asp:xmldatasource id="XmlDataSource1" runat="server" datafile="~/Books.xml" xpath="//bookstore/book"/>
</div>
</form>
</body>
</html>
Books.xml
Code
<?xml version='1.0'?>
<!-- This file is a part 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>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>
<?xml version='1.0'?>
<!-- This file is a part 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>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>
例2:数据源为:SqlDataSource1
Code
<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" DataKeyField="num" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<div class="header" id="header">test 学生(这是头)</div>
</HeaderTemplate>
<SeparatorTemplate> ---------------------------------------</SeparatorTemplate>
<ItemTemplate>
<asp:Label ID="OrderIDLabel" runat="server" Text='<%# Eval("num") %>'></asp:Label>,
<asp:Label ID="OrderDateLabel" runat="server" Text='<%# Eval("age") %>'></asp:Label>,
<asp:Label ID="RequiredDateLabel" runat="server" Text='<%# Eval("name") %>'></asp:Label>,
<asp:Label ID="ShippedDateLabel" runat="server" Text='<%# Eval("sex") %>'></asp:Label>,
<br />
</ItemTemplate>
<AlternatingItemTemplate>
<asp:Label ID="OrderIDLabel" runat="server" Text='<%# Eval("num") %>'></asp:Label>,,,
<asp:Label ID="OrderDateLabel" runat="server" Text='<%# Eval("age") %>'></asp:Label>,,,
<asp:Label ID="RequiredDateLabel" runat="server" Text='<%# Eval("name") %>'></asp:Label>,,,
<asp:Label ID="ShippedDateLabel" runat="server" Text='<%# Eval("sex") %>'></asp:Label>,,,
<br />
</AlternatingItemTemplate>
<FooterTemplate>
-----尾巴-------
</FooterTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SqlConnectionStrings %>" SelectCommand="SELECT top 3 num,name,age,sex FROM [students]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" DataKeyField="num" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<div class="header" id="header">test 学生(这是头)</div>
</HeaderTemplate>
<SeparatorTemplate> ---------------------------------------</SeparatorTemplate>
<ItemTemplate>
<asp:Label ID="OrderIDLabel" runat="server" Text='<%# Eval("num") %>'></asp:Label>,
<asp:Label ID="OrderDateLabel" runat="server" Text='<%# Eval("age") %>'></asp:Label>,
<asp:Label ID="RequiredDateLabel" runat="server" Text='<%# Eval("name") %>'></asp:Label>,
<asp:Label ID="ShippedDateLabel" runat="server" Text='<%# Eval("sex") %>'></asp:Label>,
<br />
</ItemTemplate>
<AlternatingItemTemplate>
<asp:Label ID="OrderIDLabel" runat="server" Text='<%# Eval("num") %>'></asp:Label>,,,
<asp:Label ID="OrderDateLabel" runat="server" Text='<%# Eval("age") %>'></asp:Label>,,,
<asp:Label ID="RequiredDateLabel" runat="server" Text='<%# Eval("name") %>'></asp:Label>,,,
<asp:Label ID="ShippedDateLabel" runat="server" Text='<%# Eval("sex") %>'></asp:Label>,,,
<br />
</AlternatingItemTemplate>
<FooterTemplate>
-----尾巴-------
</FooterTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SqlConnectionStrings %>" SelectCommand="SELECT top 3 num,name,age,sex FROM [students]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
web.config
Code
<connectionStrings>
<add name="SqlConnectionStrings" connectionString="Data Source=(local);Initial Catalog=test;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<connectionStrings>
<add name="SqlConnectionStrings" connectionString="Data Source=(local);Initial Catalog=test;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>