- one year ago
- Aditya Patel
- 1474 Views
- 0 Comments
- ASP.NET
Introduction:
In this article we will learn how to use DataList in asp.net or what is DataList in asp.net or why
use DataList in
asp.net with example.
Description:
In Asp.Net the DataList is a Web server control it is used for display the data
in a format that you can define using templates and styles. The DataList control is also use for display the data in any repeating
structure, such as a table or custom format. The DataList control can display rows in different layouts, such as
ordering them in columns or rows. It’s support Data binding using the DataSource property, which allows you to bind to various objects,
including ADO.NET datasets or data table or System.Collections.ArrayList object.
DataList Template property
and Description:
ItemTemplate: the
content or controls of this template will be rendered once for each row in the
data source.
AlternatingItemTemplate: the
content or controls of this template will be rendered once for every other row
in the data source. Typically, you use this template to create a different look
for the alternating rows, such as a different background color than the color
that is specified in the ItemTemplate property.
SelectedItemTemplate: the
content or controls of this template will be rendered when the user selects an
item in the DataList control. Typically, you use this
template to visually distinguish the selected row with a different background
or font color. You can also expand the item by displaying additional fields
from the data source.
EditItemTemplate: Specifies
the layout of an item when it is in edit mode. This template typically contains
editing controls, such as TextBoxcontrols.
HeaderTemplate and FooterTemplate: the
content or controls of this template will be rendered at the beginning (top)
and end (bottom) of the list.
SeparatorTemplate: This template is
used to add a separator between each items of the DataList control like (using
an HR element).
Example:
DataList.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataList.aspx.cs" Inherits="AspDotNet.DataList"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 500px;">
<div style="margin-bottom:
15px;"><b>DataList Control in Asp.Net</b></div>
<asp:DataList ID="EmployeeDataList" runat="server" RepeatColumns="2" CellSpacing="2" RepeatLayout="Table">
<ItemTemplate>
<table class="table" style="width: 300px; height: 154px; border: 1px solid
#ccc;">
<tr>
<td>EmployeeID:
</td>
<td>
<%# Eval("EmployeeID")%>
</td>
</tr>
<tr>
<td>EpmloyeeName:
</td>
<td>
<%# Eval("EpmloyeeName")%>
</td>
</tr>
<tr>
<td style="vertical-align: text-bottom;">Comment:
</td>
<td>
<%# Eval("Comment")%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
DataList.aspx.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AspDotNet
{
public partial class DataList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList list = new ArrayList();
Employee emp = new Employee();
emp.EmployeeID = "EMPID0001";
emp.EpmloyeeName = "Johen Mark";
emp.Comment = "Reference site about Lorem Ipsum," +
"giving
information on its origins, as well as a random Lipsum generator.";
list.Add(emp);
emp = new Employee();
emp.EmployeeID = "EMPID0002";
emp.EpmloyeeName = "Mahesh Patel";
emp.Comment = "Reference site about Lorem Ipsum," +
"giving
information on its origins, as well as a random Lipsum generator."; ;
list.Add(emp);
emp = new Employee();
emp.EmployeeID = "EMPID0003";
emp.EpmloyeeName = "Rohan Roy";
emp.Comment = "Reference site about Lorem Ipsum," +
"giving
information on its origins, as well as a random Lipsum generator.";
list.Add(emp);
emp = new Employee();
emp.EmployeeID = "EMPID0004";
emp.EpmloyeeName = "Kamal Roy";
emp.Comment = "Reference site about Lorem Ipsum," +
"giving
information on its origins, as well as a random Lipsum generator.";
list.Add(emp);
EmployeeDataList.DataSource =
list;
EmployeeDataList.DataBind();
}
}
class Employee
{
public string EmployeeID { get; set; }
public string EpmloyeeName { get; set; }
public string Comment { get; set; }
}
}
}
OutPut:
0 comments