Repeater控件

    Repeater控件和DataList控件,能夠用來一次顯示一組數據項。好比,能夠用它們顯示一個數據表中的全部行。 
    Repeater控件徹底由模板驅動,提供了最大的靈活性,能夠任意設置它的輸出格式。DataList控件也由模板驅動,和Repeater不一樣的是,DataList默認輸出是HTML表格,DataList將數據源中的記錄輸出爲HTML表格一個個的單元格 。html


一、Repeater支持如下5種模板:c#

      ● ItemTemplate : 對每個數據項進行格式設置 (包含要爲數據源中每一個數據項都要呈現一次的 HTML 元素和控件。)。
      ● AlternatingItemTemplate : 對交替數據項進行格式設置(包含要爲數據源中每一個數據項都要呈現一次的 HTML 元素和控件。)。
      ● SeparatorTemplate : 對分隔符進行格式設置(包含在每項之間呈現的元素。)。
      ● HeaderTemplate : 對頁眉進行格式設置(包含在列表的開始處分別呈現的文本和控件。)。
      ● FooterTemplate : 對頁腳進行格式設置(包含在列表的結束處分別呈現的文本和控件。)。ui

示例一:(基本演示)this

aspx頁面:spa

<%@ 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頁面:3d

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 模板)code

aspx頁面:orm

<%@ 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模板)server

aspx頁面:xml

<%@ 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>

二、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; }

    }
}
相關文章
相關標籤/搜索