ASP.Net Repeater with C# String Array

Updated on 12 Jun 2009,
Published on 16 Jul 2008

Repeater Data Control of ASP.Net 2.0 can also display the value stored in C# String Array. You can pass the Array name to the Repeater DataSource property for binding the data stored in string array. Simply Container.DataItem can be used in the ItemTemplate of Repeater control to display the items. Repeater Control displays the C# string array items as a horizontal list. You can use CSS style properties, div layout, HTML ul, ol and li tags or HTML table layout to display the array items in Repeater Control ItemTemplate. Repeater Control consumes much less memory than ASP.Net GridView and DataList control while rendering the items on ASP.Net web page. Repeater control’s iterative behavior to generate items can be used to generate list items or table rows dynamically. You can also see the examples of ASP.Net Repeater Table or ASP.Net Repeater Div Layout.

Repeater Examples:

You can see the live samples and examples of Repeater Control from the following links:

HTML Code to Display C# String Array in ASP.Net Repeater

[code:html]<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <%# Container.DataItem %>
        <br />
    </ItemTemplate>
</asp:Repeater>[/code]

You can see that there is nothing special in the HTML source code to display Array Items in Repeater Control ItemTemplate. <%# Container.DataItem %> is used to call the item from the iteration process of Repeater Control. HTML <br /> tag is used to display each item in new line. Repeater Control will put <br /> tag after each item due to its iterative behavior.

C# Code to Bind String Array with ASP.Net Repeater

[code:c#]string[] arr1 = new string[] {
        "array item 1",
        "array item 2",
        "array item 3",
        "array item 4",
        "array item 5" };

Repeater1.DataSource = arr1;

Repeater1.DataBind();[/code]

Name of string array arr1 is passed to the DataSource property of the Repeater control.

Output:

Get the output for above discussed code from the following link:

Repeater String Array Binding

Continue to next tutorial: ASP.Net Binding Repeater ArrayList as DataSource to learn how to bind the C# ArrayList collection to Repeater control dynamically.

1 Responses to "ASP.Net Repeater with C# String Array"
Siavash
it's working...thx a lot....
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email