ASP.NET Repeater, C# Repeater - High-Flying C# Coding Tips


Sponsored Links

73058_New Scooba® 230 Floor Washing Robot + Free Shipping!

 

ASP.NET Repeater, C# Repeater

On this page we'll look at how to use the ASP.NET Repeater using C#.

The ASP.NET Repeater is a handy control when we have a group of data rows which we want to display in a certain style such as in a DataSet, DataTable or DataView.  For instance, if you have a set of data you want to display on the screen such as the results of a database query, and you want each row in the results to be styled the same way, then the ASP.NET Repeater serves this well.

The Repeater doesn't need to know how many rows are in your dataset, it just repeats a set of HTML and ASPX code which is contained inside a template element again and again whilst it goes through each row in your dataset.   The data contained in your dataset is displayed depending on which columns you specify you would like to display in the template.

ASP.NET Repeater Example 1

The following shows an example of an ASP.NET Repeater being used on an aspx page.


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

 <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "SpecialNumber") %><br />
    <%# DataBinder.Eval(Container.DataItem, "SpecialLetters") %><br />
 </ItemTemplate>

</asp:Repeater>


The ASP.NET Repeater has to have a datasource so it has some data to display, so you can bind the repeater to a DataTable or DataView for instance.

The following example uses C# to bind the repeater to a DataTable (probably best to put it in the Page_Load method so the Repeater has data when the page first loads).


// bind our datatable to our repeater
NewsListRepeater.DataSource = newsDataTable;
NewsListRepeater.DataBind();


The example provides the following output dependant on the DataTable newsDataTable containing data.


1
E
2
DE
3
CDE
4
BCDE
5
ABCDE


You specify the name of your DataColumns in the call DataBinder.Eval(Container.DataItem, "SpecialLetters"), "SpecialLetters" being our DataColumn here.

The ASP.NET Repeater uses Templates, notice in the above example the ItemTemplate is used, and this is repeated for each DataRow in the DataTable.  The ASP.NET Repeater also offers some extra Templates to provide greater flexibility.  For instance, there are also the HeaderTemplate and FooterTemplates, which appear at the top and bottom of all your ItemTemplates, suitable to provide titles. Also you have the AlternatingItemTemplate which allows you to style every 2nd DataRow differently to the first, so you could have alternating background colours for instance when displaying your data.  Also available is the SeparatorTemplate which is used between 2 templates, that could be 2 ItemTemplates or between the ItemTemplate and the AlternatingItemTemplate. It won't be used after the last ItemTemplate (or AlternatingItemTemplate).

ASP.NET Repeater Example 2

The following shows the ASP.NET Repeater used with the AlternatingItemTemplate, SeparatorTemplate, HeaderTemplate and FooterTemplates as well as the ItemTemplate.


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

 <HeaderTemplate>Funky Title<br /><br /></HeaderTemplate>

 <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "SpecialNumber") %><br />
    <%# DataBinder.Eval(Container.DataItem, "SpecialLetters") %><br />
 </ItemTemplate>

 <SeparatorTemplate>
    ------<br />
 </SeparatorTemplate>

 <AlternatingItemTemplate>
    <i><%# DataBinder.Eval(Container.DataItem, "SpecialNumber") %></i><br />
    <i><%# DataBinder.Eval(Container.DataItem, "SpecialLetters") %></i><br />
 </AlternatingItemTemplate>

 <FooterTemplate><br />End of the funky stuff</FooterTemplate>

</asp:Repeater>


In the code behind file, the following code is used to create a DataTable and then create some simple data to put into the DataTable just for demonstration purposes, which we then bind to the Repeater.  In the real world you'll be binding your Repeater to the results of a database call or maybe even an XML file.


protected void Page_Load(object sender, EventArgs e)
{
    // create a datatable
    DataTable newsDataTable = new DataTable();

    // add some columns to our datatable
    newsDataTable.Columns.Add("SpecialNumber");
    newsDataTable.Columns.Add("SpecialLetters");

    // create some rows in our data
    string _letters = "ABCDE";
    for (int i = 1; i <= 5; i++)
    {
        DataRow newsDataRow = newsDataTable.NewRow();
        newsDataRow["SpecialNumber"] = i;
        newsDataRow["SpecialLetters"] = _letters.Substring(5 -i);
        newsDataTable.Rows.Add(newsDataRow);
    }

    // bind our datatable to our repeater
    NewsListRepeater.DataSource = newsDataTable;
    NewsListRepeater.DataBind();
}


The above ASPX code with the above C# code produces the following HTML output.


Funky Title

1
E
------
2
DE
------
3
CDE
------
4
BCDE
------
5
ABCDE

End of the funky stuff


High-Flying now has an advanced techniques page using the ASP.NET Repeater, looking at some of the events you may want to use, Repeater Events