I'm going to assume you mean the ASP.NET Repeater control.
The repeater does exactly what its name suggests: it repeats a template for each item it is databound to. Optionally, you can also specify a header and/or footer template to appear before and after, respectively, the repeated items.
Essentially they are a template of HTML and/or other simple ASP.NET controls. Using a repeater is a two-step process. First, add one to your page and define the templates. Let's do an easy one:
<asp:Repeater ID="myRepeater" runat="server">
<HeaderTemplate>
<div style="font-weight: bold;">My Repeater</div>
</HeaderTemplate>
<ItemTemplate>
<div><asp:Label ID="myLabel" runat="server"/></div>
</ItemTemplate>
</asp:Repeater>
Not much to look at, but easy to follow. This will output a
div
containing a simple ASP.NET Label for each item it repeats over. The
div
is simply so that it puts each on a new line when you see it in the browser. Additionally, I've defined a
HeaderTemplate
and put some bold text there, which will appear first and above my lines of Labels. Right now the Labels are empty though, so we need to do one more step: databind to something!
You can use a
SqlDataSource
,
ObjectDataSource
, or even just a simple
List<string>
object in your code-behind. Let's do that, since it doesn't require a database connection or anything fancy.
private void Page_Load(object sender, EventArgs e)
{
List<string> myList = new List<string>();
myList.Add("Test1");
myList.Add("Test2");
myList.Add("Test3");
myRepeater.DataSource = myList;
myRepeater.DataBind();
}
I've created a simple list of three strings and had the repeater bind to that.
Now that we have some data to display, we need to change one more thing. We need to link the labels to the data so that they'll display the contents. Change the Label control in the
ItemTemplate
to look like this:
<asp:Label ID="myLabel" runat="server" Text='<%# Container.DataItem %>'/>
This uses databinding expressions to get the current item and inject it into the text property of the label. Since we're binding to a list of strings, DataItem will be simply a string. There's much more to expressions, but I suggest you read up on those separately as they are beyond the scope of this.
If you run this, you should see something like the following in your browser:
My Repeater
Test1
Test2
Test3