Repeater控件,显示数据库信息

Repeater控件,显示数据库信息.aspx

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
            DataSet dset 
= new DataSet();
            
string conn = "server=(local);Database=test;Integrated Security=true";
            
string qry = "SELECT num,age,name,sex FROM students";
            SqlDataAdapter sda 
= new SqlDataAdapter(qry, conn);
            sda.Fill(dset);
            Repeater1.DataSource 
= dset.Tables[0].DefaultView;
            Repeater1.DataBind();
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
<form id="form1" method="post" runat="server">
    
<span style="color: #ff00ff">
 Repeater显示数据库中信息1:
<br />
    
</span>
<asp:Repeater ID="Repeater1" runat="server" >
      
<HeaderTemplate>
            
<div class="header" id="header">test 学生(这是头)</div>
      
</HeaderTemplate>
      
      
<SeparatorTemplate> ---------------------------------------
      
</SeparatorTemplate>
      
      
<ItemTemplate>
        
<div class="row">
        
<%# ((DataRowView)Container.DataItem)["num"%>
        
<%# ((DataRowView)Container.DataItem)["age"%>
        
<%# ((DataRowView)Container.DataItem)["name"%>
        
<%# ((DataRowView)Container.DataItem)["sex"%>  
        
</div>
      
</ItemTemplate>
        
       
<AlternatingItemTemplate>
         
<div class="alternate">
         
<%# ((DataRowView)Container.DataItem)["num"%>
         
<%# ((DataRowView)Container.DataItem)["age"%>
         
<%# ((DataRowView)Container.DataItem)["name"%>
         
<%# ((DataRowView)Container.DataItem)["sex"%>  
         
</div>
       
</AlternatingItemTemplate>
       
       
<FooterTemplate>
         
<div class="footer">
            
<%"总共"+((DataView)Repeater1.DataSource).Count + "个学生" %>(这是尾)
         
</div>
       
</FooterTemplate>
</asp:Repeater>
    
<br />
    
<br />
    
<br />

    
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="122px" Width="643px">
    
&lt;HeaderTemplate&gt;:头
    
&lt;ItemTemplate&gt;:记录
    
&lt;AlternatingItemTemplate&gt;:轮流的记录,可以定义上一条记录不同的样式,只能用一次!
    
&lt;FooterTemplate&gt;:尾
    
    
</asp:TextBox>
    
<br />
    
<br />
    
<span style="color: #ff0066">
    Repeater显示数据库中信息2:
</span><br />
     
<div>
    
<asp:Panel ID="divOfPlanBooks" runat="server">
        
<asp:Repeater ID="Repeater2" runat="server" DataSourceID="sourceOfPlanBooks">
            
<ItemTemplate><li><a href="Default.aspx?num=<%#Eval("num")%>"><%#Eval("name")%><%#Eval("age")%></a></li></ItemTemplate>
        
</asp:Repeater>
        
<asp:SqlDataSource ID="sourceOfPlanBooks" runat="server" ConnectionString="<%$ ConnectionStrings:SqlConnectionString1 %>"
            SelectCommand
="SELECT top 2 num, name,age,sex FROM students ORDER BY num DESC">
        
</asp:SqlDataSource>
</asp:Panel>

    
</div>


</form>
</body>
</html>

 

Web.Config:

 

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

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值