DataList is a data bound list control that displays items using certain templates defined at the design time. The content of the DataList control is manipulated by using templates sections such as AlternatingItemTemplate, EditItemTemplate, FooterTemplate, HeaderTemplate, ItemTemplate, SelectedItemTemplate and SeparatorTemplate. Each of these sections has its own characteristics to be defined but at a very minimum, the ItemTemplate needs to be defined to display the items in the DataList control. Other sections can be used to provide additional look and feel to the DataList control.
The contents of the DataList control can be manipulated by using templates. The following table lists the supported templates.
The DataList control displays data items in a repeating list, and optionally supports selecting and editing the items. The content and layout of list items in DataList is defined using templates. At a minimum, every DataList must define an ItemTemplate; however, several optional templates can be used to customize the appearance of the list.
AlternatingItemTemplate: If defined, provides the content and layout for alternating items in the DataList. If not defined, ItemTemplate is used.
EditItemTemplate: If defined, provides the content and layout for the item currently being edited in the DataList. If not defined, ItemTemplate is used.
FooterTemplate: If defined, provides the content and layout for the footer section of the DataList. If not defined, a footer section will not be displayed.
HeaderTemplate: If defined, provides the content and layout for the header section of the DataList. If not defined, a header section will not be displayed.
ItemTemplate: Required template that provides the content and layout for items in the DataList.
SelectedItemTemplate: If defined, provides the content and layout for the currently selected item in the DataList. If not defined, ItemTemplate is used.
SeparatorTemplate: If defined, provides the content and layout for the separator between items in the DataList. If not defined, a separator will not be displayed.
At the very minimum, the ItemTemplate needs to be defined to display the items in the DataList control. Additional templates can be used to provide a custom look to the DataList control.
The appearance of the DataList control may be customized by setting the style properties for the different parts of the control. The following table lists the different style properties.
AlternatingItemStyle: Specifies the style for alternating items in the DataList control.
EditItemStyle: Specifies the style for the item being edited in the DataList control.
FooterStyle: Specifies the style for the footer in the DataList control.
HeaderStyle: Specifies the style for the header in the DataList control.
ItemStyle: Specifies the style for the items in the DataList control.
SelectedItemStyle: Specifies the style for the selected item in the DataList control.
SeparatorStyle: Specifies the style for the separator between the items in the DataList control.
You can also show or hide different parts of the control. The following table lists the properties that control which parts are shown or hidden.
ShowFooter: Shows or hides the footer section of the DataList control.
ShowHeader: Shows or hides the header section of the DataList control.
The display direction of a DataList control can be vertical or horizontal. Set the RepeatDirection property to specify the display direction.
The layout of the DataList control is controlled with the RepeatLayout property. Setting this property to RepeatLayout.Table will display the DataList in a table format, while RepeatLayout.Flow displays the DataList without a table structure.
Lets we saw in detail.
To create a datalist oriented application we have to construct the templates.
That is ItemTemplate and EditItemTemplate.
ItemTemplate has been created in the following manner.
Similarly
Lets we saw an example Program..
In Default.aspx page Do the following.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:DataList runat="server"
DataKeyField="ID"
ID="DataList1"
OnEditCommand="DataList1_EditCommand"
OnCancelCommand="DataList1_CancelCommand"
OnUpdateCommand="DataList1_UpdateCommand">
<EditItemTemplate>
ID: <asp:Label ID="Label1" runat="server"
Text='<%# Eval("ID") %>'>
</asp:Label>
<br />
Name: <asp:TextBox ID="textCategoryName" runat="server"
Text='<%# Eval("name") %>'>
</asp:TextBox>
<br />
Description: <asp:TextBox ID="textDescription"
runat="server"
Text='<%# Eval("description") %>'>
</asp:TextBox>
<br />
<asp:LinkButton ID="LinkButton1" runat="server"
CommandName="update" >
Save
</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="cancel" >
Cancel
</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
ID:
<asp:Label ID="IDLabel" runat="server"
Text='<%# Eval("ID") %>'></asp:Label>
<br />
name:
<asp:Label ID="nameLabel" runat="server"
Text='<%# Eval("name") %>'></asp:Label>
<br />
description:
<asp:Label ID="descriptionLabel" runat="server"
Text='<%# Eval("description") %>'></asp:Label>
<br />
<asp:LinkButton runat="server" ID="LinkButton1"
CommandName="edit" >
Edit
</asp:LinkButton><br />
<br />
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
Now Go to Default.aspx.cs:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Bind data to the Data List control when page load first time
bindList();
}
}
void bindList()
{
SqlConnection sqlcon = new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
sqlcon.ConnectionString = "data source=.;Initial Catalog=datagrid;uid=sa;password=hi";
//Open Sql server Connection
sqlcon.Open();
sqlcmd = new SqlCommand("select * from employ", sqlcon);
//Execute query and fill value in data table
SqlDataAdapter da;
da = new SqlDataAdapter(sqlcmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
//Bind data into the DataList control
DataList1.DataSource = dt;
DataList1.DataBind();
}
//Close SQl Server Connection
sqlcon.Close();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
//Get Edit Item index when user click edit button
DataList1.EditItemIndex = e.Item.ItemIndex;
bindList();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
//Leave it Item in default for reload DataList
DataList1.EditItemIndex = -1;
bindList();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
//Get User entered value in the Edit mode text boxes
string name = ((TextBox)e.Item.FindControl("textCategoryName")).Text;
string description = ((TextBox)e.Item.FindControl("textDescription")).Text;
//Get the Identity Primary DataKey value when user click update button
int eno = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
sqlcmd = new SqlCommand("update employ set name='" + name + "',description='" + description + "' where ID='" + eno + "'", sqlcon);
sqlcon.ConnectionString = "data source=.;Initial Catalog=datagrid;uid=sa;password=1soft";
sqlcon.Open();
//Execute query to perform update operation on SQL Server
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
//Set datalist default index -1 for display record
DataList1.EditItemIndex = -1;
//Bind new updated data on DataList control
bindList();
}
}
Dont Forgot to create the Database Table Design..
Happy Coding..