Saturday, 31 August 2013

Repeater Control in ASP.net..?


Repeater Control:



            In this article I'm trying to explain what is repeater control, when we use Repeater control, how to bind datasource to Repeater control and how to display the result set to Repeater control.


Use of Repeater Control:



             The Repeater control is a data-bound control that uses templates to display data. Repeater control is used to display the table structure of data same like GridView but the unique feature of Repeater control is faster execution no other controls will execute like a Repeater control. Repeater control is light weight control. Using HTML table structure we can implement the design for Repeater control. See below code snippet for designing.






Source Code:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeater.aspx.cs" Inherits="Repeater" %>    
  <!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></title>    
   <style type="text/css">    
   .style1    
   {    
    font-size: x-large;    
    color: #003300;    
   }    
   </style>    
  </head>    
  <body>    
   <form id="form1" runat="server">    
   <div align="center">    
   <span class="style1"><strong><em>Working with Repeater Control</em></strong></span><br />    
   <br />    
   <br />    
    <asp:Label ID="lblName" runat="server" Text="Dept Name :"></asp:Label>    
   <asp:TextBox ID="txtName" runat="server"></asp:TextBox>    
   <br />    
   <asp:Label ID="lblLoc" runat="server" Text="Location :"></asp:Label>    
   <asp:TextBox ID="txtLoc" runat="server"></asp:TextBox>    
   <br />    
   <br />    
   <br />    
   <asp:Button ID="btnView" runat="server" onclick="btnView_Click" Text="View" />    
   <br />    
   <br />    
   <asp:Repeater ID="Repeater1" runat="server">    
    <HeaderTemplate>    
    <table style="background-color:Blue; color: White">    
     <tr>    
     <th align="center" width="50%">    
      Department Name        
     </th>    
     <th align="center" width="50%">    
      Location    
     </th>    
     </tr>    
    </table>      
    </HeaderTemplate>    
    <ItemTemplate>    
    <table>    
     <tr>    
     <td align="center" width="50%">    
      <%#Container.ItemIndex+1 %>) <%#Eval("dname") %>    
     </td>    
     <td align="center" width="50%">    
      <%#Eval("loc") %>    
     </td>    
     </tr>    
    </table>    
    </ItemTemplate>     
   </asp:Repeater>    
   </div>    
   </form>    
  </body>    
  </html>    
  

Code Behind:

  
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 using System.Data;  
 using System.Data.SqlClient;  

 
 public partial class Repeater : System.Web.UI.Page  
 {  
   SqlConnection con = new SqlConnection("connection-string");  
   SqlDataAdapter da;  
   DataSet ds;  
   SqlCommand cmd;  
   protected void Page_Load(object sender, EventArgs e)  
   {  
     if (!IsPostBack)  
     {  
       Bind_GV();  
     }  
   }  
   protected void btnView_Click(object sender, EventArgs e)  
   {  
     Insert_Data();  
     Clear();  
   }  
   protected void Bind_GV()  
   {  
     con.Open();  
     da = new SqlDataAdapter("select deptno,dname,loc from DEPT", con);  
     ds = new DataSet();  
     da.Fill(ds);  
     con.Close();  
     Repeater1 .DataSource = ds;  
     Repeater1.DataBind();  
   }  
   protected void Insert_Data()  
   {  
     con.Open();  
     da = new SqlDataAdapter("insert into Dept values(@dname,@loc)", con);  
     cmd = new SqlCommand("insert into Dept values(@dname,@loc)", con);  
     cmd.Parameters.AddWithValue("@dname", txtName.Text);  
     cmd.Parameters.AddWithValue("@loc", txtLoc.Text);  
     cmd.ExecuteNonQuery();  
     ds = new DataSet();  
     con.Close();  
     Bind_GV();  
     Clear();  
   }  
   protected void Clear()  
   {  
     txtLoc.Text = "";  
     txtName.Text = "";  
   }  
 }
 
 

OutPut:






Conclusion:


Repeater control works faster execution, no other control will work that much. But the disadvantage is it doesn't have any built-in support for paging, editing, or sorting of the data that is rendered through one or more of its templates.