Repeater控件和DataList控件,可以用来一次显示一组数据项。比如,可以用它们显示一个数据表中的所有行。
Repeater控件完全由模板驱动,提供了最大的灵活性,可以任意设置它的输出格式。DataList控件也由模板驱动,和Repeater不同的是,DataList默认输出是HTML表格,DataList将数据源中的记录输出为HTML表格一个个的单元格 。1、Repeater支持以下5种模板:
● ItemTemplate : 对每一个数据项进行格式设置 (包含要为数据源中每个数据项都要呈现一次的 HTML 元素和控件。)。
● AlternatingItemTemplate : 对交替数据项进行格式设置(包含要为数据源中每个数据项都要呈现一次的 HTML 元素和控件。)。 ● SeparatorTemplate : 对分隔符进行格式设置(包含在每项之间呈现的元素。)。 ● HeaderTemplate : 对页眉进行格式设置(包含在列表的开始处分别呈现的文本和控件。)。 ● FooterTemplate : 对页脚进行格式设置(包含在列表的结束处分别呈现的文本和控件。)。示例一:(基本演示)
aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RepeaterDemo._Default" %>
cs页面:
using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace RepeaterDemo{ public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ListpeopleList = new List (); peopleList.Add(new People("韩兆新",24,Sex.男)); peopleList.Add(new People("XXXX", 25, Sex.女)); peopleList.Add(new People("YYYY", 20, Sex.男)); peopleList.Add(new People("ZZZZ", 23, Sex.男)); peopleList.Add(new People("AAAA", 23, Sex.女)); peopleList.Add(new People("BBBB", 18, Sex.女)); rptPeople.DataSource = peopleList; rptPeople.DataBind(); } } public enum Sex { 男 = 2, 女 = 1, }; public class People { public People(string name, uint age, Sex sex) { this.Name = name; this.Age = age; this.Sex = sex; } public string Name {get;set;} public uint Age { get; private set; } public Sex Sex { get; private set; } }}
示例二:(AlternatingItemTemplate 模板)
aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RepeaterDemo._Default" %>
示例三:(SeparatorTemplate模板)
aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RepeaterDemo._Default" %>
2、Repeater控件的嵌套:
示例一:(Repeater控件嵌套演示:操作子Repeater控件)
aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RepeaterDemo._Default" %>
cs页面:
using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace RepeaterDemo{ public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ListpeopleList = new List (); peopleList.Add(new People("韩兆新",24,Sex.男)); peopleList.Add(new People("XXXX", 25, Sex.女)); peopleList.Add(new People("YYYY", 20, Sex.男)); peopleList.Add(new People("ZZZZ", 23, Sex.男)); peopleList.Add(new People("AAAA", 23, Sex.女)); peopleList.Add(new People("BBBB", 18, Sex.女)); rptPeople.DataSource = peopleList; rptPeople.DataBind(); } protected void rptPeople_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { List literaryList = new List (); literaryList.Add("《借我一生》"); literaryList.Add("《追风筝的人》"); literaryList.Add("《山居笔记》"); List scientificList = new List (); scientificList.Add("《时间简史》"); scientificList.Add("《果壳中的宇宙》"); scientificList.Add("《时空的未来》"); List philosophicList = new List (); philosophicList.Add("《周易正义》"); philosophicList.Add("《苏菲的世界》"); philosophicList.Add("《理想国》"); Repeater rptLiterary = e.Item.FindControl("rptLiterary") as Repeater; rptLiterary.DataSource = literaryList; rptLiterary.DataBind(); Repeater rptScientific = e.Item.FindControl("rptScientific") as Repeater; rptScientific.DataSource = scientificList; rptScientific.DataBind(); Repeater rptPhilosophic = e.Item.FindControl("rptPhilosophic") as Repeater; rptPhilosophic.DataSource = philosophicList; rptPhilosophic.DataBind(); } } } public enum Sex { 男 = 2, 女 = 1, }; public class People { public People(string name, uint age, Sex sex) { this.Name = name; this.Age = age; this.Sex = sex; } public string Name {get;set;} public uint Age { get; private set; } public Sex Sex { get; private set; } }}
示例二:(Repeater控件嵌套:获取父Repeater控件的值)
aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RepeaterDemo._Default" %>
cs页面:
using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace RepeaterDemo{ public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ListpeopleList = new List (); peopleList.Add(new People("韩兆新",24,Sex.男)); peopleList.Add(new People("XXXX", 25, Sex.女)); peopleList.Add(new People("YYYY", 20, Sex.男)); peopleList.Add(new People("ZZZZ", 23, Sex.男)); peopleList.Add(new People("AAAA", 23, Sex.女)); peopleList.Add(new People("BBBB", 18, Sex.女)); rptPeople.DataSource = peopleList; rptPeople.DataBind(); } protected void rptPeople_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { People people = e.Item.DataItem as People; string str = people.Name + "读:"; List literaryList = new List (); literaryList.Add(str + "《借我一生》"); literaryList.Add(str + "《追风筝的人》"); literaryList.Add(str + "《山居笔记》"); List scientificList = new List (); scientificList.Add(str + "《时间简史》"); scientificList.Add(str + "《果壳中的宇宙》"); scientificList.Add(str + "《时空的未来》"); List philosophicList = new List (); philosophicList.Add(str + "《周易正义》"); philosophicList.Add(str + "《苏菲的世界》"); philosophicList.Add(str + "《理想国》"); Repeater rptLiterary = e.Item.FindControl("rptLiterary") as Repeater; rptLiterary.DataSource = literaryList; rptLiterary.DataBind(); Repeater rptScientific = e.Item.FindControl("rptScientific") as Repeater; rptScientific.DataSource = scientificList; rptScientific.DataBind(); Repeater rptPhilosophic = e.Item.FindControl("rptPhilosophic") as Repeater; rptPhilosophic.DataSource = philosophicList; rptPhilosophic.DataBind(); } } } public enum Sex { 男 = 2, 女 = 1, }; public class People { public People(string name, uint age, Sex sex) { this.Name = name; this.Age = age; this.Sex = sex; } public string Name {get;set;} public uint Age { get; private set; } public Sex Sex { get; private set; } }}