Create Datalist using ASP.NET

The DataList control is, like the Repeater control, 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.

To provide good customization in the appearance and rendered markup when displaying multiple records, ASP.NET 2.0 offers the DataList and Repeater controls. The DataList and Repeater controls render their content using templates, instead of BoundFields, CheckBoxFields, ButtonFields, and so on.

Now we are going to start with one sample example:

Default.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title>Untitled Page</title>  
  7.     </head>  
  8.   
  9.     <body>  
  10.         <form id="form1" runat="server">  
  11.             <div>  
  12.                 <table>  
  13.                     <tr>  
  14.                         <td colspan="2">Student data Information</td>  
  15.                     </tr>  
  16.                     <tr>  
  17.                         <td style="height: 26px">  
  18.                             <asp:Label ID="lbl1" Text="Roll No:-" runat="server"></asp:Label>  
  19.                         </td>  
  20.                         <td style="height: 26px">  
  21.                             <asp:TextBox ID="txtro" runat="server"></asp:TextBox>  
  22.                         </td>  
  23.                     </tr>  
  24.                     <tr>  
  25.                         <td>  
  26.                             <asp:Label ID="lbl2" Text="Name:-" runat="server"></asp:Label>  
  27.                         </td>  
  28.                         <td>  
  29.                             <asp:TextBox ID="txtname" runat="server"></asp:TextBox>  
  30.                         </td>  
  31.                     </tr>  
  32.                     <tr>  
  33.                         <td>  
  34.                             <asp:Label ID="lbl3" Text="City:-" runat="server"></asp:Label>  
  35.                         </td>  
  36.                         <td>  
  37.                             <asp:TextBox ID="txtcity" runat="server" OnTextChanged="txtcity_TextChanged"></asp:TextBox>  
  38.                         </td>  
  39.                     </tr>  
  40.                     <tr>  
  41.                         <td>  
  42.                             <asp:Button runat="server" ID="butsubmit" Text="Submit" OnClick="butsubmit_Click" />  
  43.                         </td>  
  44.                         <td>  
  45.                             <asp:Button runat="server" ID="butreset" Text="Reset" OnClick="butreset_Click" />  
  46.                         </td>  
  47.                     </tr>  
  48.                     <tr>  
  49.                         <td>  
  50.                             <asp:Button runat="server" ID="butremove" Text="Remove" OnClick="butremove_Click" ToolTip="click to romove by rollno" />  
  51.                         </td>  
  52.                         <td>  
  53.                             <asp:Button runat="server" ID="butcencle" Text="cencle" />  
  54.                         </td>  
  55.                     </tr>  
  56.                 </table>  
  57.                 <asp:Panel ID="p1" runat="server" Width="467px">  
  58.                     <table>  
  59.                         <tr>  
  60.                             <td>-:Enter the no you want to romeve the data:-</td>  
  61.                             <td>  
  62.                                 <asp:TextBox ID="txtremove" runat="server"></asp:TextBox>  
  63.                             </td>  
  64.                             <td style="width: 28px">  
  65.                                 <asp:Button ID="butok" runat="server" Text="Ok" OnClick="butok_Click" />  
  66.                             </td>  
  67.                         </tr>  
  68.                     </table>  
  69.                 </asp:Panel>  
  70.                 <asp:DataList ID="dl" runat="server" OnSelectedIndexChanged="dl_SelectedIndexChanged">  
  71.                     <HeaderTemplate>  
  72.                         <table border="2">  
  73.                             <tr>  
  74.                                 <td>rollno</td>  
  75.                                 <td>name</td>  
  76.                                 <td>city</td>  
  77.                             </tr>  
  78.                     </HeaderTemplate>  
  79.                     <ItemTemplate>  
  80.                         <tr>  
  81.                             <td>  
  82.                                 <asp:TextBox ID="txt1" Text='<%# Eval("rollno") %>' runat="server"></asp:TextBox>  
  83.                             </td>  
  84.                             <td>  
  85.                                 <asp:TextBox ID="TextBox1" Text='<%# Eval("name") %>' runat="server"></asp:TextBox>  
  86.                             </td>  
  87.                             <td>  
  88.                                 <asp:TextBox ID="TextBox2" Text='<%# Eval("city") %>' runat="server"></asp:TextBox>  
  89.                             </td>  
  90.                         </tr>  
  91.                     </ItemTemplate>  
  92.                     <FooterTemplate>  
  93.                         </table>  
  94.                     </FooterTemplate>  
  95.                 </asp:DataList>  
  96.             </div>  
  97.         </form>  
  98.     </body>  
  99.   
  100.     </html>  
Default.aspx.cs

  1. using System;  
  2. using System.Data;  
  3. using System.Data.OleDb;  
  4. using System.Configuration;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11. public partial class _Default: System.Web.UI.Page {  
  12.     private void rtv() {  
  13.         p1.Visible = false;  
  14.         OleDbConnection cn11 = new OleDbConnection();  
  15.         OleDbCommand cmd11 = new OleDbCommand();  
  16.         OleDbDataAdapter adpt11 = new OleDbDataAdapter();  
  17.         DataSet ds11 = new DataSet();  
  18.         OleDbCommandBuilder blder11 = null;  
  19.         cn11 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C://parth//datalist//App_Data//dstalistcontrol.mdb;Persist Security Info=False");  
  20.         cmd11 = new OleDbCommand("select * from student");  
  21.         cn11.Open();  
  22.         cmd11.Connection = cn11;  
  23.         adpt11.SelectCommand = cmd11;  
  24.         adpt11.Fill(ds11, "student");  
  25.         adpt11.Update(ds11, "student");  
  26.         blder11 = new OleDbCommandBuilder(adpt11);  
  27.         DataTable dt11 = new DataTable();  
  28.         DataRow dr11;  
  29.         dt11.Columns.Add(new DataColumn("rollno"typeof(Int32)));  
  30.         dt11.Columns.Add(new DataColumn("name"typeof(String)));  
  31.         dt11.Columns.Add(new DataColumn("city"typeof(String)));  
  32.         int i;  
  33.         for (i = 0; i < ds11.Tables["student"].Rows.Count; i++) {  
  34.             dr11 = dt11.NewRow();  
  35.             dr11[0] = ds11.Tables["student"].Rows[i]["rollno"].ToString();  
  36.             dr11[1] = ds11.Tables["student"].Rows[i]["name"].ToString();  
  37.             dr11[2] = ds11.Tables["student"].Rows[i]["city"].ToString();  
  38.             dt11.Rows.Add(dr11);  
  39.         }  
  40.         dl.DataSource = dt11;  
  41.         dl.DataBind();  
  42.     }  
  43.     protected void Page_Load(object sender, EventArgs e) {  
  44.         rtv();  
  45.     }  
  46.     protected void dl_SelectedIndexChanged(object sender, EventArgs e) {}  
  47.     protected void butreset_Click(object sender, EventArgs e) {  
  48.         txtro.Text = "";  
  49.         txtname.Text = "";  
  50.         txtcity.Text = "";  
  51.     }  
  52.     protected void butsubmit_Click(object sender, EventArgs e) {  
  53.         OleDbConnection cn = new OleDbConnection();  
  54.         OleDbCommand cmd = new OleDbCommand();  
  55.         OleDbDataAdapter adpt = new OleDbDataAdapter();  
  56.         DataSet ds = new DataSet();  
  57.         OleDbCommandBuilder blder = null;  
  58.         string a, b, c;  
  59.         a = txtro.Text;  
  60.         b = txtname.Text;  
  61.         c = txtcity.Text;  
  62.         int k = int.Parse(a);  
  63.         cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C://parth//datalist//App_Data//dstalistcontrol.mdb;Persist Security Info=False");  
  64.         cmd = new OleDbCommand("insert into student values(" + k + ",'" + b + "','" + c + "')");  
  65.         cn.Open();  
  66.         cmd.Connection = cn;  
  67.         adpt.SelectCommand = cmd;  
  68.         adpt.Fill(ds, "student");  
  69.         // adpt.Update(ds,"student");  
  70.         blder = new OleDbCommandBuilder(adpt);  
  71.         rtv();  
  72.     }  
  73.     protected void butremove_Click(object sender, EventArgs e) {  
  74.         p1.Visible = true;  
  75.     }  
  76.     protected void butok_Click(object sender, EventArgs e) {  
  77.         OleDbConnection cn1 = new OleDbConnection();  
  78.         OleDbCommand cmd1 = new OleDbCommand();  
  79.         OleDbDataAdapter adpt1 = new OleDbDataAdapter();  
  80.         DataSet ds1 = new DataSet();  
  81.         OleDbCommandBuilder blder1 = null;  
  82.         cn1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C://parth//datalist//App_Data//dstalistcontrol.mdb;Persist Security Info=False");  
  83.         cmd1 = new OleDbCommand("select * from student");  
  84.         cn1.Open();  
  85.         cmd1.Connection = cn1;  
  86.         adpt1.SelectCommand = cmd1;  
  87.         adpt1.Fill(ds1, "student");  
  88.         adpt1.Update(ds1, "student");  
  89.         blder1 = new OleDbCommandBuilder(adpt1);  
  90.         int j = int.Parse(txtremove.Text);  
  91.         int i;  
  92.         int m;  
  93.         m = ds1.Tables["student"].Rows.Count;  
  94.         for (i = 0; i < m; i++) {  
  95.             if (txtremove.Text == ds1.Tables["student"].Rows[i]["rollno"].ToString()) {  
  96.                 cn1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C://parth//datalist//App_Data//dstalistcontrol.mdb;Persist Security Info=False");  
  97.                 cmd1 = new OleDbCommand("delete from student where rollno=" + j + "");  
  98.                 cn1.Open();  
  99.                 cmd1.Connection = cn1;  
  100.                 adpt1.SelectCommand = cmd1;  
  101.                 adpt1.Fill(ds1, "student");  
  102.                 blder1 = new OleDbCommandBuilder(adpt1);  
  103.                 rtv();  
  104.                 p1.Visible = false;  
  105.             }  
  106.         }  
  107.     }  
  108.     protected void txtcity_TextChanged(object sender, EventArgs e) {}  
  109. }  
Like the GridView, the DataList renders as an HTML <table>, but allows for multiple data-source records to be displayed per table row. The Repeater.

Once you run this code, you will get output in tabular format.