Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
i dont know how to use repeater could u give some reference with example
Posted
Comments
rajivpande86 1-Mar-11 16:53pm    
Can you take a little bit more pain to type the same in Google?
There you would have got quicker and ample solutions too.
Do not take my advice the other way.Rather this is going to help you.
Dave Kreskowiak 1-Mar-11 21:40pm    
WHAT Repeater? You're going to have to be a lot more specific about which repeater you want to use.
Sean A. Hanley 1-Mar-11 23:18pm    
The question is tagged ASP.NET so we must assume he means System.Web.UI.WebControls.Repeater
Dave Kreskowiak 2-Mar-11 0:16am    
Crap...I missed that one.

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
 
Share this answer
 
v2
Comments
Sandeep Mewara 2-Mar-11 1:21am    
Good answer! :) 5++
Hope Repeater Control[^] will help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900