Repeater Control in ASP.Net C#

The Repeater control is a container control which allows you to create a custom list of items to present data on the web page. The Repeater control same as a gridview and datagridview. It doesn’t have built-in paging as for other control like datagridview.

Following five templates.

. HeaderTemplate :- it is used to create the Header Rows of the                         table.

. FooterTemplate :- it is used to create the Footer Rows of the                         table.

. ItemTemplate   :- it is used for formatting the items.

. AlternatingItemTemplate : alternating items formatting.

. SeparatorTemplate : it is used for separating rows and columns for                      style.

Working with Repeater Control


Open visual studio and create new website after that add a Repeater Control to a web form and then write the following code in your aspx page.

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head runat=”server”>

    <title></title>

    <style type=”text/css”>

        #form1 {

            height: 187px;

            width: 255px;

        }

    </style>

</head>

<body>

    <form id=”form1″ runat=”server”>

<h3>  Employee Details</h3>

    

  <p>

     <asp:Repeater id=”Repeater1″runat=”server”>

          <HeaderTemplate>

             <table border=1>

                <tr>

                   <td><b>Employeeid</b></td>

                   <td><b>FirstName</b></td>

                   <td><b>LastName</b></td>

                </tr>

          </HeaderTemplate>

            

          <ItemTemplate>

             <tr>

                <td> <%# DataBinder.Eval(Container.DataItem, “Employeeid”)%> </td>

                <td> <%# DataBinder.Eval(Container.DataItem, “FirstName”)%> </td>

                 <td> <%# DataBinder.Eval(Container.DataItem, “LastName”)%> </td>

             </tr>

          </ItemTemplate>

            

          <FooterTemplate>

             </table>

          </FooterTemplate>

            

       </asp:Repeater>

      

    </p>

    </form>

</body>

</html>



C# Source Code



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 _Default : System.Web.UI.Page

{

    SqlConnection cnn = newSqlConnection(“Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True”);

    protected voidPage_Load(object sender, EventArgs e)

    {

        string Sql = “select [EmployeeID],[FirstName],[LastName] from dbo.Employees  order by employeeid”;

        DataTable Repeaterdt = newDataTable();

        try

        {

            SqlDataAdapter SqlAda = newSqlDataAdapter(Sql, cnn);

            SqlAda.Fill(Repeaterdt);

            Repeater1.DataSource = Repeaterdt;

            Repeater1.DataBind();

        }

        catch (Exceptionex)

        {

            Response.Write(ex.Message.ToString());

        }

      

    }

}


Leave a Reply

Your email address will not be published. Required fields are marked *