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>
<%@ 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
- Write a value which contain comma to a CSV file in c#?
- Reading CSV File with cells containing commas c#
- Split CSV with columns may contain ‘,’ Comma C#
- [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- [Simple Way]-ASP.NET Core Upload Multiple File Web API model
- [Simple Way]- Image Upload in .NET Core Web API
- [Easy Way]-Receive File and other form data together in ASP.NET Core Web API
- Replace image in word document using C#
- How to add new rows to an existing word document table in C#
- How can I bind an Eval data-binding expression to a Label when using the asp server control DataList?
- How to get data from third party rest API in MVC 6.0 and asp net core application
- How to bind data user control inside listbox WP8
- How to bind data in asp.net c# treeview control
- how to bind dropdownlist which is inside the itemtemplate field of datalist control
- How should data be synchronized between a WinForms GUI control and the client class?
- How to bind control to the current row in DataGrid?
- WPF: How to bind to a property of another's control binding?
- How can I return data from JavaScript in a WebBrowser control to C#?
- How to bind a property from my usercontrol content control to a property?
- Adding data dynamically to a asp literal control inside a grid view
- How to display group data separately using DataList in ASP.NET?
- How can I bind a ListView Item with data outside from model in UWP?
- How can I control the root element namespace and name when serializing an IXmlSerializable object with the data contract serializer?
- How identify a primary key parameter from an ASP NET MVC Model object based on annotation [key] tag?
- How to get the full Path of the file upload control in a string variable in asp .net?
- How to test ASP NET ashx Handler With File Attached
- How to find a nested control inside of asp repeater
- How to show checked radio button in Razor edit view Asp net core mvc
- Post data from a string value to asp net core web api controller method
- How to bind enum to telerik:RadGrid in ASP WebForms
- How to display table from SQL Server using ASP NET MVC5 without using Entity Framework?
- How to Increase Width of Literal control Asp .net
- How to pass data to shared views in ASP MVC?
- How can I bind the font size in a textBox to a data source?
- How to bind a single field data to combobox from observable collection in Silverlight MVVM?
- How do I set up basic authentication on specific actions in my asp net core 3.1 controller?
- uwp: how to bind data inside DataTemplate outside of x:DataType?
- How to bind multiple data on one element (Gridview)?
- How to not bind property with JSON data
- Date Time format issue in crunchbase API using C#
- Bindable field in ViewModel
- Error while starting a WPF-Application on System-Startup
- csharp flat file database for a standalone windows application
- Use custom IServiceProvider implementation in asp.net core
- Does TryExecuteTask(task) always block?
- C# Setter and if statement
- WPF DataGrid can't be refreshed after cell edit
- How is IntPtr Marshalled?
- Book rec(s) for an ASP.NET/C# developer looking to build in JSP+Tomcat?
- How to make controls stretch and shrink when the window is resized?
- IPv4 address 0.0.0.0 and IPv6 address ::0 are unspecified addresses that cannot be used as a target address
- Object-to-bytes conversion
- WCf REST service client side config file empty
- Ways to Improve this unit of work class, related to open/closed principle and dependency injection / inversion of control
- sharing parent variable between forms
- How to keep the text color black when the toolstrip menu is disabled?
- How to set classification of an email automatically?
- Why do listboxes and treeviews load large amounts of data so slowly?
- C# How to split a string by space into few arrays