Table of contents
1.
Introduction
2.
Templates
3.
Customizing the Appearance of the DataList Control
4.
ASP.NET DataList Example
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

ASP.NET DataList

Author Abhay Trivedi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

ASP.NET DataList is a data-bound control to show and manipulate data in a web application. It is a hybrid control that can combine other ASP.Net commands, and it is present in the form. The template fields control the appearance of ASP.NET DataList.

Templates

We can manipulate the contents of the DataList control by using templates. The following table lists the supported templates.

  • AlternatingItemTemplate: It provides the layout and content for alternating items in the ASP.NET DataList. If not defined, it uses ItemTemplate.
  • EditItemTemplate: It provides the content and layout for the item currently being edited in the DataList. If not defined, it uses ItemTemplate.
  • FooterTemplate: It provides the layout and content for the footer section of the DataList. If not defined, the footer section will not be displayed.
  • HeaderTemplate: It provides the layout and content for the header section of the ASP.NET DataList. If not defined, the header section will not be displayed.
  • ItemTemplate: A required template that provides the content and layout for the DataList.
  • SelectedItemTemplate: It provides the layout and content for the currently selected item in the DataList. If not specified, it uses ItemTemplate.
  • SeparatorTemplate: If defined, provide the content and layout for the separator between items in the DataList. If not specified, we will not display a separator.

Customizing the Appearance of the DataList Control

We may customize the appearance of the DataList control by setting the style properties for the different parts of the control.

  • AlternatingItemStyle: It specifies the style for alternating items in the DataList control.
  • EditItemStyle: It defines the style for the item being edited in the DataList control.
  • EditStyle: We use it to apply styles to an EditItemTemplate.
  • FooterStyle: It specifies the style for the footer in the DataList control.
  • HeaderStyle: It defines the style for the header in the DataList control.
  • ItemStyle: It defines the style for the items in the DataList control.
  • SelectedItemStyle: It represents the style for the selected item in the DataList control.
  • SeparatorStyle: It specifies the style for the separator between the items in the DataList.

 

You can show or hide different control elements using the properties listed below:

  • ShowFooter: Shows or hides the footer section of the DataList control.
  • ShowHeader: Shows or hides the header section of the DataList control.

ASP.NET DataList Example

Here is an example of a working code of ASP.NET Datalist.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataListExample.aspx.cs"   
Inherits="DataListExample.DataListExample" %>  
<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
</head>  
<body>  
    <form id="form1" runat="server">  
        <div>  
            <p>ASP.NET DataList Example of DataTable</p>  
        </div>  
        <asp:DataList ID="DataList1" runat="server">  
            <ItemTemplate>  
                <table cellpadding="2" cellspacing="0" border="1" style="width: 200px; height: 100px;   
                border: dashed 2px #04b0ef; background-color: #fffffffa">  
                    <tr>  
                        <td>  
                            <b>ID: </b><span class="idSpan"><%# Eval("ID") %></span><br />  
                            <b>Name: </b><span class="nameSpan"><%# Eval("Name") %></span><br />  
                            <b>Email: </b><span class="emailSpan"><%# Eval("Email")%></span><br />  
                        </td>  
                    </tr>  
                </table>  
            </ItemTemplate>  
        </asp:DataList>  
    </form>  
</body>  
</html>

 

Code Behind:

// DataListExample.aspx.cs

using System;  
using System.Collections.Generic;  
using System.Data;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
namespace DataListExample  
{  
    public partial class DataListExample : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            DataTable table = new DataTable();  
            table.Columns.Add("ID");  
            table.Columns.Add("Name");  
            table.Columns.Add("Email");  
            table.Rows.Add("420", "Charlie Chaplin", "Charlie@Chaplin.com"); 
            DataList1.DataSource = table;  
            DataList1.DataBind();     
        }  
    }  
}

 

Output:

FAQs

  1. What is ASP.NET?
    ASP.NET is an open-source, server-side web development framework designed to produce dynamic web pages. Microsoft developed it to allow programmers to build active websites, applications, and services.
     
  2. What is ASP.NET in C#?
    ASP.NET is a web-based application framework developed and marketed by Microsoft to let programmers build dynamic websites. It will enable you to use a full-featured programming language such as C# or VB.NET to build web applications quickly.
     
  3. What is the difference between ASP.NET and C#?
    The difference between ASP.NET and C# is that ASP.NET is an open-source framework for web application development to create dynamic content over web pages. C# is an object-oriented, functional, generic, imperative, and component-based programming language.
     
  4. What is an ASP.NET DataList?
    The ASP.NET DataList control is a lightweight server-side control that works as a container for data items. We use it to display data into a list structure to the web pages. It shows data from the data source that is either a DataTable or a table from a database.
     
  5. What are data list controls?
    We use the ASP.NET DataList control to display a repeated list of items bound to the control. However, by default, the DataList control adds a table around the data items. The DataList control may be attached to a database table, an XML file, or another list of things.

Key Takeaways

This article teaches about ASP.NET DataList and how we use them. We saw why ASP.Net DataList could be beneficial for a developer to learn. 

Click here to read about the Top 10 web development frameworks. Click here to see other related blogs on Features Of ASP Net. Check out our Web development course and blogs on Backend Web Technologies.

If you are preparing for your DSA interviews then, Coding Ninjas Studio is a one-stop destination. This platform will help you acquire effective coding techniques and overview student interview experience in various product-based companies.

Happy Learning!

Live masterclass