DataList Control in ASP.NET: Part 2


In Part 1 of this article series we discussed how to simply display data with a DataList Control but now in this article we will discuss how to display data in multiple columns with a DataList Control.

Displaying Data in Multiple Columns with DataList Control

It is very simple to render the contents of a DataList control into a multi-column table in which each data item occupies a separate table cell. Two properties modify the layout of the HTML table rendered by the DataList control; they are:

  • RepeatColumns: The number of columns to display.
  • RepeatDirection: The direction to render the cells. Possible values are Horizontal and Vertical.

Here is the example given below to display the contents of the Movies database table in a three-column layout:

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<script runat="server">
html xmlns="">
<head id="Head1" runat="server">
    <style type="text/css">
    <form id="form1" runat="server">
    <div class="content">

        <br />
        <br />
        Directed by:
        <br />
        Box Office Totals:
        <%# Eval("BoxOfficeTotal")%>
        <br />

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:DatabaseConnectionString1 %>"
        ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
        SelectCommand="SELECT Id,Title,Director,BoxOfficeTotal FROM Movies">


Notice that the DataList control in the above example includes a RepeatColumns property that has the value 3. If we set the RepeatDirection property to the value Horizontal and do not assign a value to the RepeatColumns property, then the DataList renders its data items horizontally without end. We can display data items in multiple columns when the DataList is in Flow layout mode. In that case, "<br>" tags are used to create the row breaks.

Note: This article is continued in the next part.