Binding a Scalar Array to a DataList Demo

This demo illustrates binding a scalar array of integers to a DataList. The syntax used here will also work in the Repeater and in a DataGrid TemplateColumn.


1 1 2
3 5 8
13 21 34
55 89


Source Code
<script runat="server" language="C#">
private void Page_Load(object sender,  System.EventArgs e)
{
    int [] fib = new int[11];
    
    fib[0] = 1;
    fib[1] = 1;

    for (int i = 2; i < 11; i++)
        fib[i] = fib[i - 1] + fib[i - 2];

    dlFibs.DataSource = fib;
    dlFibs.DataBind();
}
</script>

<asp:DataList id="dlFibs" runat="server" RepeatColumns="3"
       GridLines="Both" CellPadding="10" RepeatDirection="Horizontal">
  <ItemTemplate>
    <b><# Container.DataItem %></b>
  </ItemTemplate>
</asp:DataList>

[Return to the article...]