关于Repeater的用法

Steven · 2021-09-22 12:48 · 106 views · C# · old id 83

repeater总共有5个模版

ItemTemplate

包含要为数据源中每个数据项都要呈现一次的 HTML 元素和控件。

AlternatingItemTemplate

包含要为数据源中每个数据项都要呈现一次的 HTML 元素和控件。通常,可以使用此模板为交替项创建不同的外观,例如指定一种与在ItemTemplate 中指定的颜色不同的背景色。

HeaderTemplate 和FooterTemplate

包含在列表的开始和结束处分别呈现的文本和控件。

SeparatorTemplate

包含在每项之间呈现的元素。典型的示例可能是一条直线(使用 hr 元素)。

其中ItemTemplate是最常用的,用来表示常用的数据。

Foot和Head用来那些放在最外层,只使用一次的标记。

Separtor分隔标记,主要是分隔Item数据的。

Alternate,交替显示的意思。

将数据绑定到 Repeater 控件

必须将 Repeater 控件绑定到数据源。最常用的数据源是数据源控件,如 SqlDataSource 或 ObjectDataSource控件。或者,可以将 Repeater 控件绑定到任何实现 IEnumerable 接口的类,包括 ADO.NET 数据集(DataSet类)、数据读取器(SqlDataReader 类或 OleDbDataReader 类)或大部分集合。

绑定数据时,您可以为 Repeater 控件整体指定一个数据源。向 Repeater 控件添加控件时(例如,向模板中添加 Label 或 TextBox 控件时),可以使用数据绑定语法将单个控件绑定到数据源返回的项的某个字段。下面的示例演示一个包含数据绑定 Label 控件的 ItemTemplate。

这里面说了两种常用的数据样式:一种是直接加工具栏的数据控件,另外一个是在后台通过datasource,然后databind实在绑定。

<asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    ID="SqlDataSource1"runat="server"SelectCommand="SELECT [CategoryID], [CategoryName],[Description] FROM [Categories]"></asp:SqlDataSource>


 <asp:Repeater runat="server"ID="rpTableName">

                                    <ItemTemplate>
                                        <li>
                                            <%# Eval("NameTable")%>
                                        </li>
                                    </ItemTemplate>
                                </asp:Repeater>
//这是一个repeater的后台
                DataTable dt =newDataTable();
                dt = tableName.GetTable(outerror);
                rpTableName.DataSource = dt;
                rpTableName.DataBind();
Repeater实现的IEnumerable 接口

[ComVisible(true), Guid("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
publicinterfaceIEnumerable
{
    [DispId(-4)]
    IEnumerator GetEnumerator();
}

IEnumerator接口的方法如下

[ComVisible(true), Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerator
{
    bool MoveNext();
    object Current {get; }
    void Reset();
}




usingSystem;
usingSystem.Collections;
 
public class Person
{
    public Person(stringfName,stringlName)//构造函数
    {
        this.firstName = fName;
        this.lastName = lName;
    }
 
    public string firstName;
    public string lastName;
}

 


public class People : IEnumerable
{
    privatePerson[] _people;
    public People(Person[] pArray)
    {
        _people =newPerson[pArray.Length];
 
        for(inti = 0; i < pArray.Length; i++)
        {
            _people[i] = pArray[i];
        }
    }
 
    public IEnumerator GetEnumerator()
    {
        return new PeopleEnum(_people);
    }
}

 


public class PeopleEnum : IEnumerator
{
    publicPerson[] _people;
 
    // Enumerators are positioned before the first element
    // until the first MoveNext() call.
    intposition = -1;
 
    publicPeopleEnum(Person[] list)
    {
        _people = list;
    }
 
    public bool MoveNext()
    {
        position++;
        return(position < _people.Length);
    }
 
    public void Reset()
    {
        position = -1;
    }
 
    public object Current
    {
        get
        {
            try
            {
                return_people[position];
            }
            catch(IndexOutOfRangeException)
            {
                thrownewInvalidOperationException();
            }
        }
    }
}
 
classApp
{
    staticvoidMain()
    {
        Person[] peopleArray =newPerson[3]
        {
            newPerson("John","Smith"),
            newPerson("Jim","Johnson"),
            newPerson("Sue","Rabon"),
        };
 
        People peopleList =newPeople(peopleArray);
        foreach(Person pinpeopleList)
            Console.WriteLine(p.firstName +" "+ p.lastName);
 
    }
}




← Back to home