Binding a Repeater control to a jagged array

Binding a Repeater control to a jagged array

Binding the Repeater in the code-behind is done just like with any other binding operation. Suppose we have a jagged array named myJagged (which we didn’t initialize with values in this example) and a Repeater named myRepeater:

// Initialize the string array
string[][] myJagged; // Remember to initialize the array with values
myRepeater.DataSource = GroupList;
myRepeater.DataBind();

While binding the array to the repeater is done the same way as when binding to a DataReader, displaying the values inside the array on the webpage is different than when you are binding to a DataReader. To display a value of the array in the webpage, use <%#DataBinder.Eval(Container, “DataItem[0]”)%> where 0 is the index of the array value:

<asp:repeater id="myRepeater" runat="server">
   <HeaderTemplate>
      <ul>
   </HeaderTemplate>
   <ItemTemplate>
      <li><%#DataBinder.Eval(Container, "DataItem[0]")%> <%#DataBinder.Eval(Container, "DataItem[1]")%></li>
   </ItemTemplate>
   <FooterTemplate>
      </ul>
   </FooterTemplate>
</asp:repeater>

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top