用Repeater控件显示数据

来源:互联网 发布:锅匠裁缝士兵间谍知乎 编辑:程序博客网 时间:2024/04/28 04:23
用Repeater控件显示数据


文章类别:ASP.NET技巧   发表日期:2004-6-19  星期六

网 站 计 数 器 申 请
转自:动态网制作指南 www.knowsky.com
<SCRIPT type=text/javascript><!--google_ad_client = "pub-5069998594260847";google_ad_width = 336;google_ad_height = 280;google_ad_format = "336x280_as";google_ad_channel ="3718258953";google_color_border = "A8DDA0";google_color_bg = "EBFFED";google_color_link = "0000CC";google_color_url = "008000";google_color_text = "6F6F6F";//--></SCRIPT><SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript></SCRIPT><IFRAME name=google_ads_frame marginWidth=0 marginHeight=0 src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5069998594260847&amp;dt=1098523753531&amp;lmt=1098523753&amp;format=336x280_as&amp;output=html&amp;channel=3718258953&amp;url=http%3A%2F%2Fwww.knowsky.com%2F4424.html&amp;color_bg=EBFFED&amp;color_text=6F6F6F&amp;color_link=0000CC&amp;color_url=008000&amp;color_border=A8DDA0&amp;ref=http%3A%2F%2Fwww.baidu.com%2Fbaidu%3Fword%3Drepeater%26cl%3D3&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_java=true" frameBorder=0 width=336 scrolling=no height=280 allowTransparency><img height="1" width="1" border="0" src="http://pagead2.googlesyndication.com/pagead/imp.gif?client=ca-pub-5069998594260847&dt=1098523753531&lmt=1098523753&format=336x280_as&output=html&channel=3718258953&url=http%3A%2F%2Fwww.knowsky.com%2F4424.html&color_bg=EBFFED&color_text=6F6F6F&color_link=0000CC&color_url=008000&color_border=A8DDA0&ref=http%3A%2F%2Fwww.baidu.com%2Fbaidu%3Fword%3Drepeater%26cl%3D3&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_java=true&event=noiframe" /></IFRAME>

    如果你正在使用ASP.NET,你一定对DataGrid控件非常熟悉。DataGrid控件提供了各种特性,通过这些特性可以很容易地在一个Web页面上以列表形式显示数据。但是,如果你不想使用HTML表格形式呢?此时,可以使用一个DataGrid的一个鲜为人知的兄弟控件,即Repeater控件。Repeater控件提供显示你所需要数据的灵活性。

Repeater控件是什么?
 
Repeater是一个可重复操作的控件,也就是说,它通过使用模板显示一个数据源的内容,而你可以很容易地配置这些模板。Repeater包含如标题和页脚这样的数据,它可以遍历所有的数据选项并应用到模板中。

与DataGrid和DataList控件不同,Repeater控件并不是由WebControl类派生而来。所以,它不包括一些通用的格式属性,比如控制字体,颜色,等等。然而,使用Repeater控件,HTML(或者一个样式表)或者ASP.NET类可以处理这些属性。

HTML在哪里?
Repeater控件与DataGrid (以及DataList)控件的主要区别是在于如何处理HTML。ASP.NET建立HTML代码以显示DataGrid控件,但Repeater允许开发人员决定如何显示数据。所以,你可以选择将数据显示在一个HTML表格中或者一个顺序列表中。这主要取决于你的选择,但你必须将正确的HTML插入到ASP.NET页面中。

模板与DataList一样,Repeater控件只支持模板。以下的模板可供选择:

AlternatingItemTemplate: 指定如何显示每一其它选项。
ItemTemplate: 指定如何显示选项。(AlternatingItemTemplate可以覆盖这一模板。)
HeaderTemplate: 建立如何显示标题。
FooterTemplate: 建立如何显示页脚。
SeparatorTemplate: 指定如何显示不同选项之间的分隔符。
你可以使用这些模板来显示你希望的数据。唯一具有强制性的模板是ItemTemplate,所有其它的模板都是具有选择性的。

数据
对于处理一个数据源,Repeater控件具有与DataGrid与DataList相同的属性:

DataMember:获得或者设置与 Repeater 控件绑定的相应DataSource属性的表格。

DataSource:获得或者设置为 Repeater 显示提供数据的数据源。

除此之外,还有一个Items属性,你可以通过这一属性编程访问Repeater数据中单一选项。它返回一个RepeaterItemCollection对象,为一组RepeaterItem对象的集合,代表 Repeater 数据的每一行。

ASP.NET Web数据控件还有其它一个共性:它们都使用DataBind方法来生成用户界面。调用这一方法可以返回并显示数据(假设DataSource和DataMember属性设置正确)。在查看DataBind方法之前,我们先看看如何在一个Web页面中使用一个Repeater控件

使用Repeater控件
使用Repeater控件的第一步骤是决定我们将要使用的数据源和字段。例如,我们将要使用SQL Server Northwind数据库中的Employees列表。Web页面将显示职工的完整名字,地址,以及电话号码。HTML将使用DIV标记,用 Repeater 模板来分隔内容。下面是 Web 页面的 HTML 内容:

<%@ Page language="c#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html><head>
<title>Builder.com Repeater Example</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<style>
.alternate {
FONT-WEIGHT: bold;
COLOR: black;
FONT-FAMILY: Verdana, 'Times New Roman';
BACKGROUND-COLOR: yellow
}
.row {
FONT-WEIGHT: bold;
COLOR: black;
FONT-FAMILY: Verdana, 'Times New Roman';
BACKGROUND-COLOR: white
}
.footer {
FONT-WEIGHT: bold;
COLOR: red;
FONT-FAMILY: Verdana, 'Times New Roman';
BACKGROUND-COLOR: gray
}
.header {
FONT-WEIGHT: bold;
COLOR: yellow;
FONT-FAMILY: Verdana, 'Times New Roman';
BACKGROUND-COLOR: gray
}
.box {
BORDER-RIGHT: blue groove;
BORDER-TOP: blue groove;
DISPLAY: block;
VERTICAL-ALIGN: baseline;
OVERFLOW: auto;
BORDER-LEFT: blue groove;
CURSOR: wait;
BORDER-BOTTOM: blue groove;
FONT-FAMILY: verdana;
TEXT-ALIGN: center
}
body {
background: #333;
}
</style>
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e) {
if (!IsPostBack) {
DataSet dset = new DataSet();
string conn = "server=(local);Initial Catalog=Northwind;UID=ctester;PWD=password";
string qry = "SELECT firstname, lastname, address, city, region, postalcode,
homephone FROM employees";
SqlDataAdapter sda = new SqlDataAdapter(qry, conn);
sda.Fill(dset);
Repeater1.DataSource = dset.Tables[0].DefaultView;
Repeater1.DataBind();
} }
</script></head>
<body MS_POSITIONING="GridLayout" bgColor="#00cc99">
<form id="Form1" method="post" runat="server">
<div class="box">
<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<div class="header" id="header">Northwind Employees</div>
</HeaderTemplate>
<SeparatorTemplate><hr /></SeparatorTemplate>
<ItemTemplate><div class="row">
<%# ((DataRowView)Container.DataItem)["FirstName"] %>
<%# ((DataRowView)Container.DataItem)["LastName"] %><br>
<%# ((DataRowView)Container.DataItem)["Address"] %><br>
<%# ((DataRowView)Container.DataItem)["City"] %>, 
<%# ((DataRowView)Container.DataItem)["Region"] %>  
<%# ((DataRowView)Container.DataItem)["PostalCode"] %><br>
<%# ((DataRowView)Container.DataItem)["HomePhone"] %>
</div></ItemTemplate>
<AlternatingItemTemplate><div class="alternate">
<%# ((DataRowView)Container.DataItem)["FirstName"] %>
<%# ((DataRowView)Container.DataItem)["LastName"] %><br>
<%# ((DataRowView)Container.DataItem)["Address"] %><br>
<%# ((DataRowView)Container.DataItem)["City"] %>, 
<%# ((DataRowView)Container.DataItem)["Region"] %>  
<%# ((DataRowView)Container.DataItem)["PostalCode"] %><br>
<%# ((DataRowView)Container.DataItem)["HomePhone"] %>
</div></AlternatingItemTemplate>
<FooterTemplate><div class="footer">
<%# ((DataView)Repeater1.DataSource).Count + " employees found." %>
</div></FooterTemplate>
</asp:Repeater></div></form></body></html>
 

可以注意到,每个 Repeater 行中样式表控制着文字的外观。除此之外,在网页内容中还添加了一个文本框。嵌入式 C# 代码从 Repeater 的数据源中获取相应的列。每个数据项都被转换成一个DataRowView对象以便显示。

这一页面并不是使用ASP.NET的“后台代码”性质。由于这一原因,页面引用了两个System.Data和System.Data.SqlClient空间名称。这对于使用DataRowView对象和与 SQL Server 交互访问是必需的。

当调用页面时就会触发Page_Load事件。此时会把数据源连接到 Repeater 控件上,并查询数据库。每一Repeater行的代码从潜在数据源中载入数据,并且Web页面显示这些数据。

这一设计使用了样式表(以及 HTML div 标记),所以更改外观只需要更改必要的样式表代码。为了进一步地将数据和显示分离,你可以在一个独立文件中存储样式表,并以一个HTML LINK标记来引用它们。

一个很好的选择
当与其他 ASP.NET 开发者交流时,开发人员对 Repeater 控件知之甚少,这真让我感到惊讶。虽然它不如DataGrid功能强大,但它在许多场合中仍然提供了出色的灵活性。Repeater 控件缺少编辑和排序功能,但可以通过进一步编程而实现。

 

转自:动态网制作指南 www.knowsky.com