datalist控件,xml数据源,SqlDataSource数据源

例1:数据源为xml

ContractedBlock.gif ExpandedBlockStart.gif 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>

Books.xml

ContractedBlock.gif ExpandedBlockStart.gif 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>

例2:数据源为:SqlDataSource1

ContractedBlock.gif ExpandedBlockStart.gif 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>

web.config

ContractedBlock.gif ExpandedBlockStart.gif Code
    <connectionStrings>
        
<add name="SqlConnectionStrings" connectionString="Data Source=(local);Initial Catalog=test;Integrated Security=True" providerName="System.Data.SqlClient"/>

    
</connectionStrings>

转载于:https://www.cnblogs.com/jianlinglo/archive/2009/01/15/1376466.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值