DataList Control in ASP.NET: Part 6


In Part 5 of this article series we have discussed how to edit and delete data with a DataList Control but now in this article we will discuss how to format the DataList Control to look better and to use CSS better too.

Formatting DataList Control

The DataList Control includes a rich set of properties that we can use to format the HTML rendered by the control. If we want to associate Cascading Style Sheet rules with various elements of the DataList, then we can take advantage of any of the following properties:

  • CssClass: It enables us to associate a CSS class with the DataList.
  • AlternatingItemStyle: It enables us to format every other row of the DataList.
  • EditItemStyle: It enables us to format the DataList row selected for editing.
  • FooterStyle: It enables us to format the footer row of the DataList.
  • HeaderStyle: It enables us to format the header row of the DataList.
  • ItemStyle: It enables us to format each row displayed by the DataList.
  • SelectedItemStyle: It enables us to format the selected row in the DataList.
  • SeparatorStyle: It enables us to format the row separator displayed by the DataList.

When formatting the DataList, we also need to work with the following properties:

  • GridLines: It enables us to add rules around the cells in the DataList. Possible values are None, Horizontal, Vertical, and Both.
  • ShowFooter: It enables us to show or hide the footer row.
  • ShowHeader: It enables us to show or hide the header row.
  •  UseAccessibleHeader: It enables us to render HTML <th> tags instead of <td> tags for the cells in the header row.

The page given below illustrates how to take advantage of several of these formatting properties.

<%@ 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">
        font:14px Arial,Sans-Serif;
        border-bottom:Solid 1px blue;
        border-bottom:Solid 1px blue;
    <form id="form1" runat="server">
        Book List
asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>" 
            SelectCommand="SELECT [NAME] FROM [BOOKS]">

In the above example, we have used a substantial amount of CSS to improve the presentation of the DataList Control. 

Note: This is the last part of the DataList Control article series. Please do read all parts for a complete understanding.


Up Next
    Ebook Download
    View all
    View all