Databound Controls in ASP.Net

Introduction 
 
Databound controls are used to display data to the end-user within the web applications and using databound controls allows you to manipulate the data within the web applications very easily.

Databound controls are bound to the DataSource property. Databound controls are composite controls that combine other ASP.NET Controls like Text boxes, Radio buttons, Buttons and so on.

Frequently used Databound controls:
  • Repeater
  • DataList
  • GridView
  • List View
  • Form View
Repeater
  • Repeater controls is a Databound control to just display data in the web application, using this we cannot manipulate the data; in other words, a Repeater is a read-only control.
  • Repeater is very light-weight and faster to display data compared with other controls, so whenever you just want to display a repeated list of items then use a Repeater Control.
  • Repeater control works by repeating using the data source.
  • Repeater Control appearance is controlled by its templates
Itemtemplate: An Itemtemplate represents items in a Data Source; an Itemtemplate renders in the web page as a number of records from the Datasource Collection

Alternatiningtemplate: Applies a background-color or border-styles to alternative rows in the Data Source collection

Headertemplate: HeaderTemplate is used to provide Headertext for the data source collection

Footertemplate: Display footer text to the Data Source.

Example to demonstrate Repeater Control:



Employee.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataReapterDemo.aspx.cs" Inherits="DataReapterDemo" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.         <style>  
  8.         tr {  
  9.             height:40px;  
  10.         }  
  11.     </style>  
  12.     </head>  
  13.     <body>  
  14.         <form id="form1" runat="server">  
  15.             <div>  
  16.                 <center>  
  17.                     <div style="  border: 2px solid red;text-align: left;border-radius: 2px;Padding-top: 3px;background-color: Lime;width: 500px;border-radius: 8px;font-size: 20px;">  
  18.                         <asp:Repeater ID="rp1" runat="server">  
  19.                             <HeaderTemplate>  
  20.                                 <table  style="width:500px;padding-top:0px;Background-color:Gold" >  
  21.                                     <tr>  
  22.                                         <td style="font-size: 26px;  
  23.   text-align: center;  
  24.   height: 48px;">  
  25.                                             <asp:Label ID="lblhdr" runat="server" Text = "Student Profile"></asp:Label>  
  26.                                         </td>  
  27.                                     </tr>  
  28.                                 </table>  
  29.                             </HeaderTemplate>  
  30.                             <ItemTemplate>  
  31.                                 <table style="width:500px;">  
  32.                                     <tr>  
  33.                                         <td>  
  34.                                             <asp:Label ID="lblempid1" runat="server" Text="Employee ID:"></asp:Label>  
  35.                                         </td>  
  36.                                         <td>  
  37.                                             <asp:Label ID="lblempid2" runat="server" Text='<%# Eval("EmpId") %>'>  
  38.                                             </asp:Label>  
  39.                                         </td>  
  40.                                         <td rowspan="5">  
  41.                                             <asp:Image ID="img1" runat="server" Width="100px" ImageUrl= '  
  42.                                                 <%#"~/images/" + Eval("EmpImage")   %>'/>  
  43.                                             </td>  
  44.                                         </tr>  
  45.                                         <tr>  
  46.                                             <td>  
  47.                                                 <asp:Label ID="lblempname1" runat="server" Text="Employee Name"></asp:Label>  
  48.                                             </td>  
  49.                                             <td>  
  50.                                                 <asp:Label ID="lblempname2" runat="server" Text='<%# Eval("EmpName") %>'>  
  51.                                                 </asp:Label>  
  52.                                             </td>  
  53.                                         </tr>  
  54.                                         <tr>  
  55.                                             <td>  
  56.                                                 <asp:Label  ID="lblempemailId1" runat="server" Text="Employee EmailId"></asp:Label>  
  57.                                             </td>  
  58.                                             <td>  
  59.                                                 <asp:Label ID="lblempemailId2" runat="server" Text='<%# Eval("EmpEmailId") %>'>  
  60.                                                 </asp:Label>  
  61.                                             </td>  
  62.                                         </tr>  
  63.                                         <tr>  
  64.                                             <td>  
  65.                                                 <asp:Label ID="lblempmob1" runat="server" Text="Mobile Number"></asp:Label>  
  66.                                             </td>  
  67.                                             <td>  
  68.                                                 <asp:Label ID="lblempmob2" runat="server" Text='<%# Eval("EmpMobileNum") %>'>  
  69.                                                 </asp:Label>  
  70.                                             </td>  
  71.                                         </tr>  
  72.                                         <tr>  
  73.                                             <td>  
  74.                                                 <asp:Label ID="lblempgen1" runat="server" Text="Gender"></asp:Label>  
  75.                                             </td>  
  76.                                             <td>  
  77.                                                 <asp:Label ID="lblempgen2" runat="server" Text='<%# Eval("EmpGender") %>'>  
  78.                                                 </asp:Label>  
  79.                                             </td>  
  80.                                         </tr>  
  81.                                     </table>  
  82.                             </ItemTemplate>  
  83.                             <FooterTemplate>  
  84.                                     <table>  
  85.                                         <tr>  
  86.                                             <td>     
  87.                                             @Developed by Abhishek Uppula  
  88.                                             </td>  
  89.                                         </tr>  
  90.                                     </table>  
  91.                             </FooterTemplate>  
  92.                         </asp:Repeater>  
  93.                     </div>  
  94.                 </center>  
  95.             </div>  
  96.         </form>  
  97.     </body>  
  98. </html>  
Employee.aspx.cs 
  1. using System;    
  2. using System.Web;    
  3. using System.Web.UI;    
  4. using System.Web.UI.WebControls;    
  5.     
  6. using System.Data.SqlClient;    
  7. using System.Data;    
  8. using System.Web.Configuration;    
  9.     
  10. public partial class DataReapterDemo : System.Web.UI.Page    
  11. {    
  12.     SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);    
  13.     protected void Page_Load(object sender, EventArgs e)    
  14.   {    
  15.         if (!IsPostBack)    
  16.             {    
  17.             Bind();    
  18.      }    }    
  19.         
  20.     
  21. public void Bind()    
  22.     {    
  23.         SqlCommand cmd = new SqlCommand("select * from Employee where EmpId = 1200",con);    
  24.         SqlDataAdapter da = new SqlDataAdapter(cmd);    
  25.         DataSet ds = new DataSet();    
  26.         da.Fill(ds, "Employee");    
  27.         rp1.DataSource = ds.Tables[0];    
  28.         rp1.DataBind();    
  29.     
  30.     }    
  31. }    


Similar Articles