Binding Data to Data Web Controls

4GuysFromRolla – Binding a Scalar Array to Data Web Control

The ASP.NET data Web controls can display any data that implements either the IEnumerable interface or the IListSource interface. They are bound through two lines of codes:

  • The object is assigned to the data Web control’s DataSource property
  • The data Web control’s DataBind() method is called

The syntax for displaying a particular field in a data Web control depends upon the data Web control being used.

Two examples:

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

By Bryan Xu