Tuesday, February 3, 2009

Binding a DataList to an ArrayList

The following example shows how to databind a DataList to an ArrayList containing uppercase Alpha Characters.

C# Function to create and attach ArrayList:
private void bindAlphabetList()
ArrayList al_az = new ArrayList();
for (int i = 65; i < datasource =" al_az;">

Source Code for the DataList:
<asp:DataList ID="dl_az" runat="server" RepeatDirection="Horizontal" >
<asp:LinkButton ID="lb_letter" runat="server" Text='<%# Container.DataItem %>'></asp:LinkButton>
<SeparatorTemplate> | </SeparatorTemplate>
Note how an attribute is bound to the ArrayList value:
Text='<%# Container.DataItem %>'

Alternatively an attribute can be bound to the Index of the DataListItem in which it is enclosed, as follows:
Text='<%# Container.ItemIndex %>'

or the Index of the GridViewRow if working with a GridView:
Text='<%# Container.DataItemIndex %>'

1 comment:

Anonymous said...

nice article keep it up