articles

home / developersection / articles / repeater control in asp.net

Repeater Control in ASP.Net

Pushpendra Singh 15.82 K 02-Nov-2010

The Repeater control is used to display a repeated list of items that are bound to the control.

The Repeater control allows you to create templates to define the layout of its content. The templates are:

·  ItemTemplate - Use this template for elements that are rendered once per row of data.

·  AlternatingItemTemplate - Use this template for elements that are rendered every other row of data. This allows you to alternate background colors, for example.

·   HeaderTemplate - Use this template for elements that you want to render once before your ItemTemplate section.

·   FooterTemplate - Use this template for elements that you want to render once after your ItemTemplate section.

·   SeperatorTemplate - Use this template for elements to render between each row, such as line breaks.

  
<asp:Repeater ID="Repeater1" runat="server">
</asp:Repeater>

 

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
 <table border="2">
  <tr>
 <td>
     ID <asp:Label ID="lbl1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"id") %>'></asp:Label>
     </td>
     <td>
 Password <asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"pass") %>'></asp:Label>
    </td>
    <td>
 NAME <asp:Label ID="Label2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"name") %>'></asp:Label>
    </td>
    <td>
    </td>
    </tr>
    </table>
   </ItemTemplate>
   </asp:Repeater>

BindData:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Binddata();
        }
 
    }
    public void Binddata()
    {
        DataSet ds = new DataSet();
 
        Repeater1.DataSource = Class1.execute_fill_repeater("insert_reg");
        Repeater1.DataBind();
 
    }

In above code execute_fill_repeater 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

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_ repeater(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();
 
        SqlDataAdapter da = new SqlDataAdapter(spname, con);
        da.Fill(ds);
 
        return ds;
    }
}

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;
    }
 
   
}

 

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
 
as
begin
select * from regform
end

Updated 04-Mar-2020

Leave Comment

Comments

Liked By