DataList Control in ASP.Net

Introduction

DataList is a Databound control to display and manipulate data in a web application. It is a composite control that can combine other ASP.Net controls and it is present in the form. The DataList appearance is controlled by its template fields.

The following template fields are supported by the DataList control:

  • Itemtemplate: It specifies the Items present in the Datasource, it renders itself in the browser as many rows present in the data source collection.
  • EditItemTemplate: Used to provide edit permissions to the user.
  • HeaderTemplate: Used to display header text to the data source collection.
  • FooterTemplate: Used to display footer text to the data source collection.
  • ItemStyle: Used to apply styles to an ItemTemplate.
  • EditStyle: Used to apply styles to an EditItemTemplate
  • HeaderStyle: Used to apply styles to a HeaderTemplate
  • FooterStyle: Used to apply styles to a FooterTemplate.
Frequently used properties with DataList



Frequently used Events with DataList control

Event: Event is a precise time that something happened.



The following  is an example demonstrate the DataList Control with a bound image and a FileUpload control:



Default.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html>  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.         <style>  
  8.         .itemstyle {  
  9.               border-collapse: collapse;  
  10.   background-color: aquamarine;  
  11.   border: 1px solid red;  
  12.   border-radius: 8px;  
  13.   padding:4px;  
  14.         }  
  15.         tr {  
  16.             height:40px;  
  17.         }  
  18.         table {  
  19.               width: initial;  
  20.         }  
  21.     </style>  
  22.     </head>  
  23.     <body>  
  24.         <form id="form1" runat="server">  
  25.             <div>  
  26.                 <asp:DataList ID="dl1" runat="server"  
  27.      RepeatDirection="Horizontal"  
  28.       RepeatColumns="3"  
  29.        OnEditCommand="dl1_EditCommand"  
  30.         OnCancelCommand="dl1_CancelCommand"  
  31.          OnUpdateCommand="dl1_updateCommand"  
  32.           OnDeleteCommand="dl1_DeleteCommand" >  
  33.                     <HeaderTemplate>  
  34.                         <table>  
  35.                             <tr>  
  36.                                 <td>  
  37.                                     <h1>Student Information</h1>  
  38.                                 </td>  
  39.                             </tr>  
  40.                         </table>  
  41.                     </HeaderTemplate>  
  42.                     <ItemStyle CssClass="itemstyle" />  
  43.                     <ItemTemplate>  
  44.                         <table border="1">  
  45.                             <tr>  
  46.                                 <td>  
  47.     Employee ID:  
  48.     </td>  
  49.                                 <td>  
  50.                                     <asp:Label ID="lblempid" runat="server" Text='<%# Eval("EmpId") %>'>  
  51.                                     </asp:Label>  
  52.                                 </td>  
  53.                                 <td rowspan="5">  
  54.                                     <asp:Image ID="empimg" runat="server" ImageUrl='  
  55.                                         <%# "~/Images/" + Eval("EmpImage") %>Height="180px" Width="200px" />  
  56.                                     </td>  
  57.                                 </tr>  
  58.                                 <tr>  
  59.                                     <td>  
  60.     Employee Name:  
  61.     </td>  
  62.                                     <td>  
  63.                                         <asp:Label ID="lblempname" runat="server" Text='<%# Eval("EmpName") %>'>  
  64.                                         </asp:Label>  
  65.                                     </td>  
  66.                                 </tr>  
  67.                                 <tr>  
  68.                                     <td>  
  69.     Employee EmailId:  
  70.     </td>  
  71.                                     <td>  
  72.                                         <asp:Label ID="lblemailid" runat="server" Text='<%# Eval("EmpEmailId") %>'>  
  73.                                         </asp:Label>  
  74.                                     </td>  
  75.                                 </tr>  
  76.                                 <tr>  
  77.                                     <td>  
  78.      Employee Mobile Number  
  79.     </td>  
  80.                                     <td>  
  81.                                         <asp:Label ID="lblmbnum" runat="server" Text='<%# Eval("EmpMobileNum")  %>'>  
  82.                                         </asp:Label>  
  83.                                     </td>  
  84.                                 </tr>  
  85.                                 <tr >  
  86.                                     <td colspan="3" style="text-align:left">  
  87.                
  88.       
  89.                                         <asp:Button ID="btn1" runat="server" CommandName="edit" Text="Edit" />  
  90.           
  91.       
  92.                                         <asp:Button ID="btn2" runat="server" Text="Delete" CommandName="delete" />  
  93.                                     </td>  
  94.                                 </tr>  
  95.                             </table>  
  96.                         </ItemTemplate>  
  97.                         <EditItemStyle CssClass="itemstyle" />  
  98.                         <EditItemTemplate>  
  99.                             <table border="1">  
  100.                                 <tr>  
  101.                                     <td>  
  102.     Employee Id:  
  103.     </td>  
  104.                                     <td>  
  105.                                         <asp:TextBox ID="txtempid" ReadOnly="true" runat="server" Text='<%# Eval("EmpId") %>'>  
  106.                                         </asp:TextBox>  
  107.                                     </td>  
  108.                                     <td rowspan="5">  
  109.                                         <asp:Image ID="empimg" runat="server" ImageUrl='  
  110.                                             <%# "~/Images/" + Eval("EmpImage") %>Height="180px" />  
  111.                                             <br />  
  112.                                             <asp:FileUpload ID="fu1" runat="server" />  
  113.                                         </td>  
  114.                                     </tr>  
  115.                                     <tr>  
  116.                                         <td>  
  117.      Employee Name  
  118.     </td>  
  119.                                         <td>  
  120.                                             <asp:TextBox ID="txtempname" runat="server" Text='<%# Eval("EmpName") %>'>  
  121.                                             </asp:TextBox>  
  122.                                         </td>  
  123.                                     </tr>  
  124.                                     <tr>  
  125.                                         <td>  
  126.     Employee EmailId  
  127.     </td>  
  128.                                         <td>  
  129.                                             <asp:TextBox ID="txtempmail" runat="server" Text='<%# Eval("EmpEmailId") %>'>  
  130.                                             </asp:TextBox>  
  131.                                         </td>  
  132.                                     </tr>  
  133.                                     <tr>  
  134.                                         <td>  
  135.     Employee Mobile Number  
  136.     </td>  
  137.                                         <td>  
  138.                                             <asp:TextBox ID="txtmbnum" runat="server" Text='<%# Eval("EmpMobileNum") %>'>  
  139.                                             </asp:TextBox>  
  140.                                         </td>  
  141.                                     </tr>  
  142.                                     <tr>  
  143.                                         <td colspan="3" style="text-align:left;">  
  144.                
  145.       
  146.                                             <asp:Button ID="btn3" runat="server" CommandName="update" Text="update" />  
  147.            
  148.       
  149.                                             <asp:Button ID="btn4" runat="server" CommandName="cancel" Text="Cancel" />  
  150.                                         </td>  
  151.                                     </tr>  
  152.                                 </table>  
  153.                             </EditItemTemplate>  
  154.                         </asp:DataList>  
  155.                     </div>  
  156.                 </form>  
  157.             </body>  
  158.         </html>  
Default.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. using System.Data.SqlClient;  
  9. using System.Data;  
  10. using System.Web.Configuration;  
  11.   
  12. public partial class _Default : System.Web.UI.Page  
  13. {  
  14.   
  15.     SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);  
  16.     protected void Page_Load(object sender, EventArgs e)  
  17.     {  
  18.         if (!IsPostBack)  
  19.         {  
  20.             Bind();  
  21.         }  
  22.     }  
  23.     public void Bind()  
  24.     {  
  25.         SqlCommand cmd = new SqlCommand("select * from Employee", con);  
  26.         SqlDataAdapter da = new SqlDataAdapter(cmd);  
  27.         DataSet ds = new DataSet();  
  28.         da.Fill(ds, "Employee");  
  29.         dl1.DataSource = ds.Tables[0];  
  30.         dl1.DataBind();  
  31.     }  
  32.   
  33.     protected void dl1_EditCommand(object sender, DataListCommandEventArgs e)  
  34.     {  
  35.         dl1.EditItemIndex = e.Item.ItemIndex;  
  36.         Bind();  
  37.   
  38.     }  
  39.     protected void dl1_CancelCommand(object sender, DataListCommandEventArgs e)  
  40.     {  
  41.         dl1.EditItemIndex = -1;  
  42.         Bind();  
  43.     }  
  44.     protected void dl1_updateCommand(object sender, DataListCommandEventArgs e)  
  45.     {  
  46.         int index = e.Item.ItemIndex;  
  47.   
  48.         int empid = Convert.ToInt32(((TextBox)dl1.Items[index].FindControl("txtempid")).Text);  
  49.         string empname = ((TextBox)dl1.Items[index].FindControl("txtempname")).Text;  
  50.         string empmail = ((TextBox)dl1.Items[index].FindControl("txtempmail")).Text;  
  51.         long empmbnum = Convert.ToInt64(((TextBox)dl1.Items[index].FindControl("txtmbnum")).Text);  
  52.         FileUpload fu = (FileUpload)dl1.Items[index].FindControl("fu1");  
  53.   
  54.          
  55.        // to update image name in data base and image in server, If he selectted new Image   
  56.           
  57.         if (fu.HasFile)  
  58.         {  
  59.             string filepath = System.IO.Path.Combine(Server.MapPath("~/Images/"), fu.FileName);  
  60.             fu.SaveAs(filepath);  
  61.   
  62.             SqlCommand fcmd = new SqlCommand();  
  63.             fcmd.CommandText = "update employee set EmpImage='"+fu.FileName+"'";  
  64.             fcmd.Connection = con;  
  65.   
  66.             con.Open();  
  67.             fcmd.ExecuteNonQuery();  
  68.             con.Close();  
  69.   
  70.         }  
  71.   
  72.         SqlCommand cmd = new SqlCommand("update Employee set EmpName = '" + empname + "',EmpEmailId='" + empmail + "',EmpMobileNum =" + empmbnum + " where EmpId = " + empid + "", con);  
  73.         con.Open();  
  74.         int i = cmd.ExecuteNonQuery();  
  75.         con.Close();  
  76.         if (i == 1)  
  77.         {  
  78.             Response.Write("<script>alert('Successfully updated')</script>");  
  79.             dl1.EditItemIndex = -1;  
  80.             Bind();  
  81.         }  
  82.   
  83.   
  84.   
  85.     }  
  86.     protected void dl1_DeleteCommand(object sender, DataListCommandEventArgs e)  
  87.     {  
  88.         int index = e.Item.ItemIndex;  
  89.         Label empid;  
  90.         empid = (Label)dl1.Items[index].FindControl("lblempid");  
  91.   
  92.         SqlCommand cmd = new SqlCommand("delete from employee where EmpId = " + Convert.ToInt32(empid.Text) + "", con);  
  93.         con.Open();  
  94.         int res = cmd.ExecuteNonQuery();  
  95.         con.Close();  
  96.         if (res == 1)  
  97.         {  
  98.             Response.Write("<script>alert('Successfully deleted')</script>");  
  99.             Bind();  
  100.         }  
  101.   
  102.     }  
  103. }  

Up Next
    Ebook Download
    View all
    Learn
    View all