The ASP.NET DataList control is a more customizable version of the Gridview and a lightweight server-side control that works as a container for data items. DataList is used to display data into a list format but needs More manual work as you have to design it yourself. It does not support paging,sorting feature.you have to do it manually.

The DataList control has four templates.

HeaderTemplate – The content of HeaderTemplate template will not be render this is head section of the DataList.
ItemTemplate – The content of ItemTemplate template will be render for each  data item in DataSource.
SeparatorTemplate – SeparatorTemplate template is used to add a separator between two items in DataList.
FooterTemplate – The content of FooterTemplate template will not be repeat.

  • Open visual studio add new empty website
  • File>>New>>Web Site then select >”Empty Web Site”

Now I have added a Datalist control on the page.I want to display the name and email of the user in the Datalist control.I have added a table in the ItemTemplate which will be repeated for each item in the dataset.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:DataList ID="DataList1" runat="server" Width="518px">
            <ItemTemplate>
                    <table class="auto-style1" style="width:100%">
        <tr style="background-color: #666699; height: 50px;">
         <td>  Name <asp:Label ID="lblname" runat="server" style="float:right" Text='<%#Eval("name") %>'></asp:Label></td>
        </tr>
        <tr style="background-color: #FFCCFF;height: 50px;">
         <td>  Email<asp:Label ID="lblemail" runat="server" style="float:right" Text='<%#Eval("email") %>'></asp:Label></td>
        </tr>
    </table>
            </ItemTemplate>
        </asp:DataList>
      </form>

</body>
</html>

 


Now I have created a dummy table (for testing purposes ) add this table into Dataset(dataset is collection of the table) and make this dataset as datasource to the Datalist control.

You can also bind Datalist control from the database I have created a dummy dataset for testing purposes.

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindDatalist();

    }
    public void BindDatalist()
    {

        DataSet ds = new DataSet();

        DataTable dttable = new DataTable();
        dttable.Columns.Add("name");
        dttable.Columns.Add("email");
        dttable.Rows.Add(new object[] {"jacob","testing@gmail.com" });
        dttable.Rows.Add(new object[] { "zoltan", "zoltan@gmail.com" });
        ds.Tables.Add(dttable);
        DataList1.DataSourceID = null;
        DataList1.DataSource = ds.Tables[0].DefaultView;
        DataList1.DataBind();
    }
}
run your application:-

How to bound the header template of the datalist control to a data item?
DataList header template example

<asp:DataList ID="DataList1" runat="server" Width="518px">
      <HeaderTemplate><asp:Label ID="lblname" runat="server" style="background-color:brown;height:80px;" Text="UserInformation"></asp:Label></HeaderTemplate>
            <ItemTemplate>
   <table class="auto-style1" style="width:100%">
        <tr style="background-color: #666699; height: 50px;">
         <td>  Name <asp:Label ID="lblname" runat="server" style="float:right" Text='<%#Eval("name") %>'></asp:Label></td>
        </tr>
        <tr style="background-color: #FFCCFF;height: 50px;">
         <td>  Email<asp:Label ID="lblemail" runat="server" style="float:right" Text='<%#Eval("email") %>'></asp:Label></td>
        </tr>
    </table>
    </ItemTemplate>
  </asp:DataList>

 

Displaying Data in Multiple Columns with DataList Control contents of a DataList control repeat in the table cell.
Properties that modify the layout of the HTML table rendered by the DataList control:-
RepeatColumns: The number of columns to display.
RepeatDirection: The direction to repeat the cells. Horizontal or Vertical. Displaying data in two columns
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:DataList ID="DataList1" runat="server" Width="518px" RepeatColumns="3">
      <HeaderTemplate >UserInformation</HeaderTemplate>
            <ItemTemplate>
   <table class="auto-style1" style="width:100%">
        <tr style="background-color: #666699; height: 50px;">
         <td>  Name <asp:Label ID="lblname" runat="server" style="float:right" Text='<%#Eval("name") %>'></asp:Label></td>
        </tr>
        <tr style="background-color: #FFCCFF;height: 50px;">
         <td>  Email<asp:Label ID="lblemail" runat="server" style="float:right" Text='<%#Eval("email") %>'></asp:Label></td>
        </tr>
    </table>
            </ItemTemplate>
        </asp:DataList>  
    </form>
</body>
</html>

 

How to bind data from sql server to datalist using asp.net

If you want to bind the DataList Control from database first create table tbluser in database.

CREATE TABLE [dbo].[tbluser](
[Userid] [int] IDENTITY(1,1) NOT NULL,
[Username] [varchar](100) NULL,
[password] [varchar](100) NULL,
[Address] [varchar](100) NULL
)
select * from tbluser


You want Bind user details to a DataList control then create connection string in the web.config

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:DataList ID="DataList1" runat="server" Width="518px">
            <ItemTemplate>
   <table class="auto-style1" style="width:100%">
        <tr style="background-color: #666699; height: 50px;">
         <td>  UserName <asp:Label ID="lblname" runat="server" style="float:right" Text='<%#Eval("Username") %>'></asp:Label></td>
        </tr>
        <tr style="background-color: #FFCCFF;height: 50px;">
         <td>  Userpassword<asp:Label ID="lblemail" runat="server" style="float:right" Text='<%#Eval("password") %>'></asp:Label></td>
        </tr>
        <tr style="background-color: #FFCCFF;height: 50px;">
         <td>  UserAddress<asp:Label ID="Label1" runat="server" style="float:right" Text='<%#Eval("Address") %>'></asp:Label></td>
        </tr>
    </table>
            </ItemTemplate>
        </asp:DataList>
  
    </form>
</body>
</html>

Now bind DataList control from database

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindDatalist();

    }
    public void BindDatalist()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
        SqlCommand cmd = new SqlCommand("Select * from tbluser", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            DataList1.DataSourceID = null;
            DataList1.DataSource = ds.Tables[0].DefaultView;
            DataList1.DataBind();
        }
      
    }
}

The post How to Bind Data in DataList Control in Asp.Net – C# appeared first on Software Development | Programming Tutorials.



Read More Articles