How To Use DataList Control In ASP.NET With Example in C#

Introduction

In this article, I’ll present a tutorial with an example: a way to use the DataList control in ASP.NET for displaying information/records from the database using SQL DataSource.

So, first, we will discuss ASP.NET data list control.

What is a data list control in ASP.NET ?

The DataList control, just like the Repeater control, maybe a model-driven, light-weight control that acts as an instrumentality of continual information. The templates during this control area unit define the information that it'll contain. It is versatile in the sense that you simply will easily customize the display of 1 or many records that area unit displayed within the management. You've got a property within the DataList management known as RepeatDirection that may be used to customize the layout of the control.

For creating a database, you can follow this SQL query as per your requirement.  In this article, I am creating one temp table with some duplicate records for demonstrating the data list control.

Create Table

  1. CREATE TABLE #Employee(  
  2.    EmpID INT,  
  3.    EmpName NVARCHAR(50),  
  4.    EmpDept NVARCHAR(50),  
  5.    EmpCity NVARCHAR(50),  
  6.    Salary NVARCHAR(50),  
  7.    Designation NVARCHAR(50)  
  8. )  

Insert Data into Table

  1. INSERT INTO #Employee (EmpID, EmpName, EmpDept, EmpCity, Salary, Designation)VALUES ('1','NIKUNJ SATASIYA','ASP.NET','SURAT','RS 18000','TL')  
  2. INSERT INTO #Employee (EmpID, EmpName, EmpDept, EmpCity, Salary, Designation)VALUES ('1','HIREN DOBARIYA','ASP.NET','KHAMTA','RS 19000','TL')  
  3. INSERT INTO #Employee (EmpID, EmpName, EmpDept, EmpCity, Salary, Designation)VALUES ('1','VIVEK GHADIYA','ANDROID','JAMNAGAR','RS 37000','TL')  
  4. INSERT INTO #Employee (EmpID, EmpName, EmpDept, EmpCity, Salary, Designation)VALUES ('1','SNEHAL PATEL','IOS','SURAT','RS 45000','TL')  
  5. INSERT INTO #Employee (EmpID, EmpName, EmpDept, EmpCity, Salary, Designation)VALUES ('1','PRATIK PANSURIYA','JAVA','RAJKOT','RS 33000','TL')  
  6. INSERT INTO #Employee (EmpID, EmpName, EmpDept, EmpCity, Salary,Designation)VALUES ('1','SARAH DEMOLA','PHP','SURAT','RS 19000','TL')  

Get  Data From Table

  1. SELECT * FROM #Employee  

Drop Table

  1. DROP TABLE #Employee   
  • Table – this is often the default layout. The Table layout renders the DataList as associate hypertext markup language (HTML) Table and it renders within the Table Cell. The quantity of Cells in every row will be set using the Repeat Columns property.

  • Flow – The Flow layout doesn't render the DataList as any markup language component. All the DataList things are rendered directly even as for how the Repeater control repeats its things.

  • OrderedList and UnorderedList – The OrderedList and UnorderedList layouts render the DataList as markup language Ordered List and Unordered List. But in .Net 4.0 and higher than these aren't supported any longer.

  • DataList management and Templates -The DataList control makes use of the subsequent templates.

  • HeaderTemplate – The content of this example won't be recurrent and can be placed in the topmost position, i.e., head section of the DataList control.

  • ItemTemplate – The contents of this example are recurrent for every record present in its DataSource.

  • AlternatingItemTemplate – AlternatingItemTemplate is used for adding alternate things. It's used together with ItemTemplate, usually for displaying a unique style for alternating things.

  • SeparatorTemplate - This example is used to feature an extractor between 2 things of the DataList control.

  • FooterTemplate – The content of this example won't be continual and can be placed at the bottom-most position i.e. footer section of the DataList control.

HTML Markup

The following markup language consists of associate ASP.NET DataList control and ASP.NET SqlDataSource control.

The SqlDataSource control is about with the subsequent properties.

  1. connectionString 
         Name of the Connection String setting within the web.Config file.
  1. <configuration>  
  2.  <connectionStrings>  
  3.    <add name="constr" connectionString="Data Source=.\SQL2005;Initial Catalog=northwind;integrated security=true"/>  
  4.  </connectionStrings>  
  5.  <system.web>  
  6.    <compilation debug="true" targetFramework="4.0"/>  
  7.  </system.web>  
  8. lt;/configuration>  
  1. selectCommand

    The Select statement is to fetch the records from Employee table of the Northwind database.

    The ID of the SqlDataSource control is about as DataSourceID of the DataList control.
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CS" %>  
    2.   
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    4. <html xmlns="http://www.w3.org/1999/xhtml">  
    5. <head runat="server">  
    6.     <title></title>  
    7.     <style type="text/css">  
    8.         body  
    9.         {  
    10.             font-family: Arial;  
    11.             font-size: 10pt;  
    12.         }  
    13.         .table  
    14.         {  
    15.             border: 1px solid #ccc;  
    16.             border-collapse: collapse;  
    17.             width: 200px;  
    18.         }  
    19.         .table th  
    20.         {  
    21.             background-color: #F7F7F7;  
    22.             color: #333;  
    23.             font-weight: bold;  
    24.         }  
    25.         .table th, .table td  
    26.         {  
    27.             padding: 5px;  
    28.             border: 1px solid #ccc;  
    29.         }  
    30.     </style>  
    31. </head>  
    32. <body>  
    33.     <form id="form1" runat="server">  
    34.     <asp:DataList ID="dlCustomers" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="3"  
    35.         CellSpacing="3" RepeatLayout="Table">  
    36.         <ItemTemplate>  
    37.             <table class="table">  
    38.                 <tr>  
    39.                     <th colspan="2">  
    40.                         <b>  
    41.                             <%# Eval("EmpName") %></b>  
    42.                     </th>  
    43.                 </tr>  
    44.                 <tr>  
    45.                     <td colspan="2">  
    46.                         <%# Eval("EmpDept") %>,  
    47.                         <%# Eval("EmpCity") %><br />  
    48.                     </td>  
    49.                 </tr>  
    50.                 <tr>  
    51.                     <td>  
    52.                         Salary:  
    53.                     </td>  
    54.                     <td>  
    55.                         <%# Eval("Salary")%>  
    56.                     </td>  
    57.                 </tr>  
    58.                 <tr>  
    59.                     <td>  
    60.                         Designation:  
    61.                     </td>  
    62.                     <td>  
    63.                         <%# Eval("Designation")%>  
    64.                     </td>  
    65.                 </tr>  
    66.             </table>  
    67.         </ItemTemplate>  
    68.     </asp:DataList>  
    69.     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:constr %>"  
    70.         SelectCommand="SELECT TOP 3 * FROM YOUR_TABL_ENAME"></asp:SqlDataSource>  
    71.     </form>  
    72. </body>  
    73. </html>  

Output Screen

Output Screen


Similar Articles
Codingvila
Codingvila is an educational website, developed to help tech specialists/beginners.