---
title: "DataList Control in ASP.Net"  
description: "The DataList control is used to display a repeated list of items that are bound to the control. However, the DataList control adds a table around the"  
author: "Pushpendra Singh"  
published: 2010-10-30  
updated: 2020-03-07  
canonical: https://www.mindstick.com/articles/185/datalist-control-in-asp-dot-net  
category: "ado.net"  
tags: ["ado.net"]  
reading_time: 5 minutes  

---

# DataList Control in ASP.Net

The DataList control is used to display a repeated list of items that are bound to the control. However, the DataList control adds a table around the data items by default.The DataList control may be bound to a database table, an XML file, or another list of items. \

<asp:DataList ID="DataList1" runat="server"> </asp:DataList>

##### Bind Data in DataList:

```
<asp:DataList ID="DataList1" runat="server"          oneditcommand="DataList1_EditCommand"         onupdatecommand="DataList1_UpdateCommand"         oncancelcommand="DataList1_CancelCommand"         ondeletecommand="DataList1_DeleteCommand"> <HeaderTemplate> </HeaderTemplate> <ItemTemplate> <asp:Label ID="lbl1" runat="server" Text= '<%# DataBinder.Eval(Container.DataItem, "id") %> '> </asp:Label> <%# DataBinder.Eval(Container.DataItem, "pass") %> <%# DataBinder.Eval(Container.DataItem, "name") %>  <asp:LinkButton id="LinkButton1"runat="server"Text="edit"CommandName="edit"/><asp:LinkButton id="LinkButton2"runat="server"Text="Delete"CommandName="Delete"/></ItemTemplate>  <EditItemTemplate> Item: <asp:TextBox id="Text1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "id") %>'/> Item: <asp:TextBox id="Text2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "pass") %>'/> Item: <asp:TextBox id="Text3" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "name") %>' /><asp:LinkButton id="button1"runat="server"Text="Update"CommandName="update"/><asp:LinkButton id="button2"runat="server"Text="Cancel"CommandName="cancel"/></EditItemTemplate>  <FooterTemplate><asp:TextBox ID="txt1" runat="server"></asp:TextBox><asp:TextBox ID="txt2" runat="server"></asp:TextBox><asp:TextBox ID="txt3" runat="server"></asp:TextBox><asp:LinkButton ID="lnkbtn1" runat="server" CommandName="ADD">ADD</asp:LinkButton></FooterTemplate>  </asp:DataList>    <asp:Label ID="lbl1" runat="server"  Text= '<%# DataBinder.Eval(Container.DataItem, "id") %> '> </asp:Label> <%# DataBinder.Eval(Container.DataItem, "pass") %> <%# DataBinder.Eval(Container.DataItem, "name") %>
```

\

DataBinder.Eval takes the argument. The first arg is the data object to bind to. The second arg is the string name of the field which will display on the page and return the data corresponding to the second argument.

![DataList Control in ASP.Net](https://www.mindstick.com/mindstickarticle/22647a24-09bc-437e-a95f-203a8dd1880f/images/7a5585aa-6d4f-4002-9eec-f192de0df219.png)

```
protected void Page_Load(object sender, EventArgs e){ if (!IsPostBack) {Binddata();// this function is called when the page is loaded. }}  public  void Binddata(){DataSet ds = new  DataSet();         DataList1.DataSource = Class1.execute_fill_datalist("insert_reg");// Here data is fetched from the database through stored procedure(insert_reg).   DataList1.DataBind();// Here data is bind to the control and show on page}
```

In above code execute_fill_datalist is a function created in Class1.cs which is described below.

And insert_reg is a stored procedure made in backend.Which is described below

![DataList Control in ASP.Net](https://www.mindstick.com/mindstickarticle/22647a24-09bc-437e-a95f-203a8dd1880f/images/a6b6d2ae-22b8-4f63-9c16-e73b8b77879f.png)

##### DataList Event

![DataList Control in ASP.Net](https://www.mindstick.com/mindstickarticle/22647a24-09bc-437e-a95f-203a8dd1880f/images/063e663e-ec5f-47f7-92ac-db81bd9dae50.png)

##### Editing DataList Items

The DataList control supports in-place editing of the data in an item through its EditItemTemplate property. The EditItemTemplate defines the content and appearance of the item when it is being edited.

```
// Edit command is used to editing in DataListprotected  void DataList1_EditCommand(object source,DataListCommandEventArgse)    {//Set the EditItemIndex property to the index of the item clicked//  in the DataList control to enable editing for that item. //  Rebind the DataList to the data source to refresh the control. DataList1.EditItemIndex = (int)e.Item.ItemIndex; Binddata();}// CancelCommand is used to cancel editing.protected void DataList1_CancelCommand(objectsource,DataListCommandEventArgse){// Set the EditItemIndex property to -1 to exit editing mode.And rebind the data. DataList1.EditItemIndex = -1; Binddata();}
```

![DataList Control in ASP.Net](https://www.mindstick.com/mindstickarticle/22647a24-09bc-437e-a95f-203a8dd1880f/images/6cc2f105-c346-4add-b958-dc91f31e087f.png)

##### Add, Update and Delete Data in DataList:

The Item Command event is raised whenever any button associated with a row in the DataList is clicked. This provides for programmatically determining which specific command button is clicked and take appropriate action.

Here this event is used for Adding data

```
protected  void DataList1_ItemCommand(object source,DataListCommandEventArgse)    {if (e.CommandName == "ADD"){// Here   FindControl will find the TextboxId in which adding will be done.string i = ((TextBox)e.Item.FindControl("txt1")).Text;string ps = ((TextBox)e.Item.FindControl("txt2")).Text;string nm = ((TextBox)e.Item.FindControl("txt3")).Text;  Class1.update_reg("add_reg", i, ps, nm);DataList1.EditItemIndex = -1;Binddata(); }}  // UpdateCommand is used to update data  protected void DataList1_UpdateCommand(object source,  DataListCommandEventArgs e){// Here   FindControl will find the Textbox in which updating will be done.string i = ((TextBox)e.Item.FindControl("Text1")).Text;string ps = ((TextBox)e.Item.FindControl("Text2")).Text;string nm = ((TextBox)e.Item.FindControl("Text3")).Text;  // update_reg is a function which fetch the data from database through stored procedure.  Class1.update_reg("update_reg",i,ps, nm); DataList1.EditItemIndex = -1; Binddata();}// DeleteCommand is used to delete data  protected void DataList1_DeleteCommand(object source,  DataListCommandEventArgs e){ string dl = ((Label)e.Item.FindControl("lbl1")).Text;Class1.Delete_reg("del_reg", dl); Binddata();}
```

![DataList Control in ASP.Net](https://www.mindstick.com/mindstickarticle/22647a24-09bc-437e-a95f-203a8dd1880f/images/5fa6024a-9eea-4780-9cff-559af7f40f8c.png)

![DataList Control in ASP.Net](https://www.mindstick.com/mindstickarticle/22647a24-09bc-437e-a95f-203a8dd1880f/images/04c58865-03e9-4800-a9c6-d77e01691f61.png)

Add both namespace in every .cs page in which you want to use data and SQL related keyword

using System.Data;

using System.Data.SqlClient;

##### Class1.cs

```
using System;using System.Collections.Generic;using System.Linq;using System.Data.SqlClient;using System.Data;using System.Web;  public class Class1{           public static DataSet execute_fill_datalist(string spname)// this function is used to bind data    {         SqlConnection con = Connection.CreateConnection();         DataSet ds = new DataSet();         SqlCommand cmd = new SqlCommand();         cmd.CommandText = spname;         cmd.CommandType = CommandType.StoredProcedure;         cmd.Connection = con;         con.Open();// connection is open         SqlDataAdapter da =  new SqlDataAdapter(spname, con);         da.Fill(ds);           return ds;    }     public static void updat_reg(string spname,string id,string ps,string nm)// this function is used to update and add data    {         SqlConnection con=Connection.CreateConnection();         SqlCommand cmd = new SqlCommand();         cmd.CommandText = spname;         cmd.CommandType = CommandType.StoredProcedure;         cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = id;         cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = ps;         cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = nm;           cmd.Connection = con;         con.Open();         cmd.ExecuteNonQuery();    }      public static void delete_regform(string spname, string id)  // this function is used to delete data    {         SqlConnection con = Connection.CreateConnection();         SqlCommand cmd = new SqlCommand();         cmd.CommandText = spname;         cmd.CommandType = CommandType.StoredProcedure;         cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = id;                  cmd.Connection = con;         con.Open();         cmd.ExecuteNonQuery();    }}
```

In class1.cs **Connection** keyword is used

Connection is class in whichCreateConnection function is created.

##### Connection.cs

```
using System;using System.Collections.Generic;using System.Linq;using System.Configuration;using System.Web;using System.Data;using System.Data.SqlClient;  public class Connection{  public static SqlConnection CreateConnection() {              string connectionstring;connectionstring = ConfigurationManager.ConnectionStrings["mycon"].ConnectionString;SqlConnection sqlcon = new  SqlConnection(connectionstring);return sqlcon; // Here connection string will be return }    }
```

mycon is connection object created in web.config

##### web.config:

```
<connectionStrings><add name="mycon" connectionString="Data Source=(local);Initial Catalog=my; User Id=sa; Password=sa" providerName="System.Data.SqlClient"/>      </connectionStrings>
```

\

Write this connection string above <system.web>

##### Stored procedure:

##### This procedure is used to select data

```
Create procedure insert_reg  asbeginselect * from regformend
```

##### This procedure is used to Add data

```
Create procedure add_reg@id varchar(50),@pass varchar(50),@name varchar(50)asbegininsert into regform(id,pass,name)values(@id,@pass,@name)End
```

##### This procedure is used to Delete data

```
create procedure del_reg@id varchar(50)asbegindelete  from regform where id=@idend
```

##### This procedure is used to Update data

```
create procedure [dbo].[update_reg]@id varchar(50),@pass varchar(50),@name varchar(50)asbeginupdate regform set pass=@pass,name=@name where id=@idend
```

---

Original Source: https://www.mindstick.com/articles/185/datalist-control-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
