关于Repeater的用法
发布日期:2021-08-19 01:52:50 浏览次数:2 分类:技术文章

本文共 3853 字,大约阅读时间需要 12 分钟。

Repeater Web 服务器控件:服务器控件是一个数据绑定容器控件,用于生成各个项的列表。

这句话就是说repeater是用来循环生成某些标记的。一定程度上相当于for循环

您可以从页的任何可用数据中创建出自定义列表。Repeater 控件不具备内置的呈现功能,这表示用户必须通过创建模板为 Repeater 控件提供布局。当该页运行时,Repeater 控件依次通过数据源中的记录,并为每个记录呈现一个项。

上面的话是说,repeater是最简单的控件,需要添加模版才能显示数据。

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实在绑定。

repeater//这是repeater的前台                                
  • <%# Eval("NameTable")%>
  • //这是一个repeater的后台 DataTable dt = new DataTable(); dt = tableName.GetTable(out error); rpTableName.DataSource = dt; rpTableName.DataBind();

     

    这是Repeater实现的IEnumerable借口

    [ComVisible(true), Guid("496B0ABE-CDEE-11d3-88E8-00902754C43A")]public interface IEnumerable{    [DispId(-4)]    IEnumerator GetEnumerator();}
     
    [DispId(-4)]IEnumerator GetEnumerator();
    通过IEnumerator定义了了GetEnumerator方法
     
    IEnumerator接口的方法如下
    [ComVisible(true), Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")]public interface IEnumerator{    bool MoveNext();    object Current { get; }    void Reset();}
    定义了三个方法,一个为MoveNext,一个为Current,这个正好和foreach对应的,所以从这个角度来理解foreach自然就容易啦。
     
    ();
    ();
    ();
    三个子方法。
    这是微软官方的一个例子
    using System;using System.Collections;public class Person{    public Person(string fName, string lName)//构造函数    {        this.firstName = fName;        this.lastName = lName;    }    public string firstName;    public string lastName;}public class People : IEnumerable{    private Person[] _people;    public People(Person[] pArray)    {        _people = new Person[pArray.Length];        for (int i = 0; i < pArray.Length; i++)        {            _people[i] = pArray[i];        }    }    public IEnumerator GetEnumerator()    {        return new PeopleEnum(_people);    }}public class PeopleEnum : IEnumerator{    public Person[] _people;    // Enumerators are positioned before the first element    // until the first MoveNext() call.    int position = -1;    public PeopleEnum(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)            {                throw new InvalidOperationException();            }        }    }}class App{    static void Main()    {        Person[] peopleArray = new Person[3]        {            new Person("John", "Smith"),            new Person("Jim", "Johnson"),            new Person("Sue", "Rabon"),        };        People peopleList = new People(peopleArray);        foreach (Person p in peopleList)            Console.WriteLine(p.firstName + " " + p.lastName);    }}相信通过这个程序就可以很好的理解啦。
     

    转载于:https://www.cnblogs.com/zhulinmails/archive/2012/04/15/2451067.html

    转载地址:https://blog.csdn.net/weixin_30911809/article/details/96883815 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

    上一篇:git的使用
    下一篇:【转载】C++——CString用法大全

    发表评论

    最新留言

    初次前来,多多关照!
    [***.217.46.12]2024年04月22日 18时31分37秒