Wednesday, November 18, 2009

Bind ArrayList Text and Value Field to DataList

Description:
Here is the code for binding the text and the value fields of the ArrayList to a DataList in Asp.net using C#.

HTML:
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<%# ((ListItem)Container.DataItem).Text %> ..........<%# ((ListItem)Container.DataItem).Value %>
</ItemTemplate>
</asp:DataList>

Code behind:
ArrayList aa = new ArrayList();
aa.Add(new ListItem("John", "1"));
aa.Add(new ListItem("Smit", "2"));
aa.Add(new ListItem("Bob", "3"));
DataList1.DataSource = aa;
DataList1.DataBind();