关于Repeater控件的使用

** 关于 Repeater控件的使用 **


Repeater控件是一个数据显示控件,该控件允许通过为列表中显示的每一项重复使用指定的模板来自定义布局。

要显示数据,必须先创建模板来绑定数据列表,模块定义如下(另见SDK):

** 模板 **

|

** 说明 **

---|---

AlternatingItemTemplate

|

与 ** ItemTemplate ** 元素类似,但在 Repeater 控件中隔行(交替项)呈现一次。通过设置 AlternatingItemTemplate 元素的样式属性,可以为其指定不同的外观。

FooterTemplate

|

在所有数据绑定行呈现之后呈现一次的元素。典型的用途是关闭在 ** HeaderTemplate ** 项中打开的元素(使用 ** ** 这样的标记)。

** 注意 ** FooterTemplate 不能是数据绑定的。

HeaderTemplate

|

在所有数据绑定行呈现之前呈现一次的元素。典型的用途是开始一个容器元素(如表)。

** 注意 ** HeaderTemplate 项不能是数据绑定的。

ItemTemplate

|

为数据源中的每一行都呈现一次的元素。若要显示 ** ItemTemplate ** 中的数据,请声明一个或多个 Web 服务器控件并设置其数据绑定表达式以使其计算为 Repeater 控件(即容器控件)的 DataSource 中的字段。以下示例显示一个示例声明,它显示包含 Label 控件中的第一个名称的字段。

First Name:
1<asp:label runat="server" text="```
2# Container.DataItem.FirstName 
3```"></asp:label>

SeparatorTemplate

|

在各行之间呈现的元素,通常是分行符( **

1<br/>

** 标记)、水平线( **

1<hr/>

** 标记)等。

** 注意 ** SeparatorTemplate 项不能是数据绑定的。

注:该控件是不能通过可视化编辑模板的,而 DataList的DataGrid控件就可以。

下面说一下程序的创建过程:

1、 创建一个 WEB应用程序,将默认的WEB窗体改名为:Repeater.aspx。

2、 切换到“ HTML”视图,输入下列代码:

1@ Page language="c#" Codebehind="Repeater.aspx.cs" AutoEventWireup="false" Inherits="TeachShow.Charpter7.Repeater" 
 1<html>
 2<head>
 3<title>Repeater</title>
 4<link href="../Style.css" rel="stylesheet" type="text/css"/>
 5<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
 6<meta content="C#" name="CODE_LANGUAGE"/>
 7<meta content="JavaScript" name="vs_defaultClientScript"/>
 8<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"/>
 9</head>
10<body ms_positioning="GridLayout">
11<form id="Form1" method="post" runat="server">
12<div align="center">
13<center>
14<table border="0" cellpadding="0" cellspacing="0" height="136" width="272">
15<tr>
16<td height="136" width="272">
17<div align="center">
18<center>
19<table border="1" bordercolordark="#ffffff" bordercolorlight="#000000" cellpadding="0" cellspacing="0" class="smallRed" height="60" width="272">
20<asp:repeater id="Repeater1" runat="server">
21<headertemplate>
22<tr>
23<td height="30" width="90"><font face="宋体">数字</font></td>
24<td height="30" width="91"><font face="宋体">平方</font></td>
25<td height="30" width="91"><font face="宋体">立方</font></td>
26</tr>
27</headertemplate>
28<itemtemplate>
29<tr>
30<td height="30" width="90">```
31# DataBinder.Eval(Container.DataItem,"数字") 
32```</td>
33<td height="30" width="91">```
34# DataBinder.Eval(Container.DataItem,"平方") 
35```</td>
36<td height="30" width="91">```
37# DataBinder.Eval(Container.DataItem,"立方") 
38```</td>
39</tr>
40</itemtemplate>
41</asp:repeater>
42</table>
43</center>
44</div>
45</td>
46</tr>
47</table>
48</center>
49</div>
50</form>
51</body>
52</html>

解释一下程序中用到的方法:

DataBinder.Eval()方法: 该方法用于在运行时计算数据绑定表达式,并且根据浏览器的需要来格式化输出结果。该方法有三个参数:

A、 数据项的命名容器:命名容器是一个对象引用,该对象即是计算表达式所针对的对象。如果绑定是针对列表控件(如 Repeater、DataList或DataGrid)的,则命名容器将始终是Container.DataItem。如果绑定是针对页面的,则命名容器是Page。

B、 数据字段名:绑定表格的列名(此例如“平方”等)。

C、 格式字符串

如果要求高性能,不建议使用 DataBinder.Eval()方法

3、 打开 Repeater.aspx.cs文件,输入下面的代码:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

namespace TeachShow.Charpter7

{

///

1<summary>
2
3///  Repeater 的摘要说明。 
4
5///  </summary>

public class Repeater : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Repeater Repeater1;

private void Page_Load( object sender, System.EventArgs e)

{

// 在此处放置用户代码以初始化页面

if (! this .IsPostBack)

{

DataTable mydt= new DataTable();

DataRow mydr;

mydt.Columns.Add( new DataColumn( "数字" , typeof (Int32)));

mydt.Columns.Add( new DataColumn( "平方" , typeof (Int32)));

mydt.Columns.Add( new DataColumn( "立方" , typeof (Int32)));

for ( int i=0;i<=10;i++)

{

mydr=mydt.NewRow();

mydr[0]=i;

mydr[1]=i*i;

mydr[2]=iii;

mydt.Rows.Add(mydr);

}

this .Repeater1.DataSource=mydt;

this .Repeater1.DataBind();

}

}

#region Web 窗体设计器生成的代码

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。

//

InitializeComponent();

base .OnInit(e);

}

///

1<summary>
2
3///  设计器支持所需的方法 - 不要使用代码编辑器修改 
4
5///  此方法的内容。 
6
7///  </summary>

private void InitializeComponent()

{

this .Repeater1.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler( this .Repeater1_ItemCommand);

this .Load += new System.EventHandler( this .Page_Load);

}

<font size="2

Published At
Categories with Web编程
Tagged with
comments powered by Disqus