Repeater控件_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > Repeater控件

Repeater控件

 2014/4/7 10:16:36  博客园(韩兆新)  博客园  我要评论(0)
  • 摘要:   Repeater控件和DataList控件,可以用来一次显示一组数据项。比如,可以用它们显示一个数据表中的所有行。    Repeater控件完全由模板驱动,提供了最大的灵活性,可以任意设置它的输出格式。DataList控件也由模板驱动,和Repeater不同的是,DataList默认输出是HTML表格,DataList将数据源中的记录输出为HTML表格一个个的单元格。1、Repeater支持以下5种模板: 
  • 标签:Repeater 控件

    Repeater控件和DataList控件,可以用来一次显示一组数据项。比如,可以用它们显示一个数据表中的所有行。 
    Repeater控件完全由模板驱动,提供了最大的灵活性,可以任意设置它的输出格式。DataList控件也由模板驱动,和Repeater不同的是,DataList默认输出是HTML表格,DataList将数据源中的记录输出为HTML表格一个个的单元格 。


1、Repeater支持以下5种模板:

      ● ItemTemplate : 对每一个数据项进行格式设置 (包含要为数据源中每个数据项都要呈现一次的 HTML 元素和控件。)。
      ● AlternatingItemTemplate : 对交替数据项进行格式设置(包含要为数据源中每个数据项都要呈现一次的 HTML 元素和控件。)。
      ● SeparatorTemplate : 对分隔符进行格式设置(包含在每项之间呈现的元素。)。
      ● HeaderTemplate : 对页眉进行格式设置(包含在列表的开始处分别呈现的文本和控件。)。
      ● FooterTemplate : 对页脚进行格式设置(包含在列表的结束处分别呈现的文本和控件。)。

示例一:(基本演示)

aspx页面:

class="xml:nogutter:nocontrols"><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RepeaterDemo._Default" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Repeater ID="rptPeople" runat="server">
            <HeaderTemplate>
            <table border="1">
                <tr>
                    <td>姓名</td>
                    <td>年龄</td>
                    <td>性别</td>
                </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td><%#DataBinder.Eval(Container.DataItem,"Name") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Age") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Sex") %></td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
            </table>
            </FooterTemplate>
        </asp:Repeater>
    
    </div>
    </form>
</body>
</html>

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)
        {
            List<People> peopleList = new List<People>();
            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" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Repeater ID="rptPeople" runat="server">
            <HeaderTemplate>
            <table border="1">
                <tr>
                    <td>姓名</td>
                    <td>年龄</td>
                    <td>性别</td>
                </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td><%#DataBinder.Eval(Container.DataItem,"Name") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Age") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Sex") %></td>
                </tr>
            </ItemTemplate>
            <AlternatingItemTemplate>
                <tr style="background:gray">
                    <td><%#DataBinder.Eval(Container.DataItem,"Name") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Age") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Sex") %></td>
                </tr>
            </AlternatingItemTemplate>
            <FooterTemplate>
            </table>
            </FooterTemplate>
        </asp:Repeater>
    
    </div>
    </form>
</body>
</html>

示例三:(SeparatorTemplate模板)

aspx页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RepeaterDemo._Default" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Repeater ID="rptPeople" runat="server">
            <HeaderTemplate>
            <table border="1">
                <tr>
                    <td>姓名</td>
                    <td>年龄</td>
                    <td>性别</td>
                </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td><%#DataBinder.Eval(Container.DataItem,"Name") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Age") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Sex") %></td>
                </tr>
            </ItemTemplate>
            <AlternatingItemTemplate>
                <tr style="background:gray">
                    <td><%#DataBinder.Eval(Container.DataItem,"Name") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Age") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Sex") %></td>
                </tr>
            </AlternatingItemTemplate>
            <SeparatorTemplate>
                <tr style="background:red">
                    <td>123</td>
                </tr>
            </SeparatorTemplate>
            <FooterTemplate>
            </table>
            </FooterTemplate>
        </asp:Repeater>
    
    </div>
    </form>
</body>
</html>

2、Repeater控件的嵌套:

示例一:(Repeater控件嵌套演示:操作子Repeater控件)

aspx页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RepeaterDemo._Default" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Repeater ID="rptPeople" runat="server" 
            onitemdatabound="rptPeople_ItemDataBound">
            <HeaderTemplate>
            <table border="1">
                <tr>
                    <td>姓名</td>
                    <td>年龄</td>
                    <td>性别</td>
                    <td>书籍类别</td>
                    <td>书籍名称</td>
                </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td><%#DataBinder.Eval(Container.DataItem,"Name") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Age") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Sex") %></td>
                    <td>
                        <table>
                            <tr><td>文学类:</td></tr>
                            <tr><td>科学类:</td></tr>
                            <tr><td>哲学类:</td></tr>
                        </table>
                    </td>
                    <td>
                        <table>
                            <tr>
                            <asp:Repeater ID="rptLiterary" runat="server">
                                <ItemTemplate>
                                <td><%#Container.DataItem %></td>
                                </ItemTemplate>
                            </asp:Repeater>
                            </tr>
                            <tr>
                            <asp:Repeater ID="rptScientific" runat="server">
                                <ItemTemplate>
                                <td><%#Container.DataItem %></td>
                                </ItemTemplate>
                            </asp:Repeater>
                            </tr>
                            <tr>
                            <asp:Repeater ID="rptPhilosophic" runat="server">
                                <ItemTemplate>
                                <td><%#Container.DataItem %></td>
                                </ItemTemplate>
                            </asp:Repeater>
                            </tr>
                        </table>
                    </td>
                </tr>
            </ItemTemplate>
            <AlternatingItemTemplate>
                <tr style="background:gray">
                    <td><%#DataBinder.Eval(Container.DataItem,"Name") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Age") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Sex") %></td>
                    <td>
                        <table>
                            <tr><td>文学类:</td></tr>
                            <tr><td>科学类:</td></tr>
                            <tr><td>哲学类:</td></tr>
                        </table>
                    </td>
                    <td>
                        <table>
                            <tr>
                            <asp:Repeater ID="rptLiterary" runat="server">
                                <ItemTemplate>
                                <td><%#Container.DataItem %></td>
                                </ItemTemplate>
                            </asp:Repeater>
                            </tr>
                            <tr>
                            <asp:Repeater ID="rptScientific" runat="server">
                                <ItemTemplate>
                                <td><%#Container.DataItem %></td>
                                </ItemTemplate>
                            </asp:Repeater>
                            </tr>
                            <tr>
                            <asp:Repeater ID="rptPhilosophic" runat="server">
                                <ItemTemplate>
                                <td><%#Container.DataItem %></td>
                                </ItemTemplate>
                            </asp:Repeater>
                            </tr>
                        </table>
                    </td>
                </tr>
            </AlternatingItemTemplate>
            <FooterTemplate>
            </table>
            </FooterTemplate>
        </asp:Repeater>
    
    </div>
    </form>
</body>
</html>

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)
        {
            List<People> peopleList = new List<People>();
            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<string> literaryList = new List<string>();
                literaryList.Add("《借我一生》");
                literaryList.Add("《追风筝的人》");
                literaryList.Add("《山居笔记》");
                List<string> scientificList = new List<string>();
                scientificList.Add("《时间简史》");
                scientificList.Add("《果壳中的宇宙》");
                scientificList.Add("《时空的未来》");
                List<string> philosophicList = new List<string>();
                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" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Repeater ID="rptPeople" runat="server" 
            onitemdatabound="rptPeople_ItemDataBound">
            <HeaderTemplate>
            <table border="1">
                <tr>
                    <td>姓名</td>
                    <td>年龄</td>
                    <td>性别</td>
                    <td>书籍类别</td>
                    <td>书籍名称</td>
                </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td><%#DataBinder.Eval(Container.DataItem,"Name") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Age") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Sex") %></td>
                    <td>
                        <table>
                            <tr><td>文学类:</td></tr>
                            <tr><td>科学类:</td></tr>
                            <tr><td>哲学类:</td></tr>
                        </table>
                    </td>
                    <td>
                        <table>
                            <tr>
                            <asp:Repeater ID="rptLiterary" runat="server">
                                <ItemTemplate>
                                <td><%#Container.DataItem %></td>
                                </ItemTemplate>
                            </asp:Repeater>
                            </tr>
                            <tr>
                            <asp:Repeater ID="rptScientific" runat="server">
                                <ItemTemplate>
                                <td><%#Container.DataItem %></td>
                                </ItemTemplate>
                            </asp:Repeater>
                            </tr>
                            <tr>
                            <asp:Repeater ID="rptPhilosophic" runat="server">
                                <ItemTemplate>
                                <td><%#Container.DataItem %></td>
                                </ItemTemplate>
                            </asp:Repeater>
                            </tr>
                        </table>
                    </td>
                </tr>
            </ItemTemplate>
            <AlternatingItemTemplate>
                <tr style="background:gray">
                    <td><%#DataBinder.Eval(Container.DataItem,"Name") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Age") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Sex") %></td>
                    <td>
                        <table>
                            <tr><td>文学类:</td></tr>
                            <tr><td>科学类:</td></tr>
                            <tr><td>哲学类:</td></tr>
                        </table>
                    </td>
                    <td>
                        <table>
                            <tr>
                            <asp:Repeater ID="rptLiterary" runat="server">
                                <ItemTemplate>
                                <td><%#Container.DataItem %></td>
                                </ItemTemplate>
                            </asp:Repeater>
                            </tr>
                            <tr>
                            <asp:Repeater ID="rptScientific" runat="server">
                                <ItemTemplate>
                                <td><%#Container.DataItem %></td>
                                </ItemTemplate>
                            </asp:Repeater>
                            </tr>
                            <tr>
                            <asp:Repeater ID="rptPhilosophic" runat="server">
                                <ItemTemplate>
                                <td><%#Container.DataItem %></td>
                                </ItemTemplate>
                            </asp:Repeater>
                            </tr>
                        </table>
                    </td>
                </tr>
            </AlternatingItemTemplate>
            <FooterTemplate>
            </table>
            </FooterTemplate>
        </asp:Repeater>
    
    </div>
    </form>
</body>
</html>

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)
        {
            List<People> peopleList = new List<People>();
            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<string> literaryList = new List<string>();
                literaryList.Add(str + "《借我一生》");
                literaryList.Add(str + "《追风筝的人》");
                literaryList.Add(str + "《山居笔记》");
                List<string> scientificList = new List<string>();
                scientificList.Add(str + "《时间简史》");
                scientificList.Add(str + "《果壳中的宇宙》");
                scientificList.Add(str + "《时空的未来》");
                List<string> philosophicList = new List<string>();
                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; }

    }
}
上一篇: 奶茶MM微软入职邮件曝光 已加入Bing团队 下一篇: 没有下一篇了!
发表评论
用户名: 匿名