What is Repeater Control in Asp.Net...?

Introduction
Here, I will explain, What is Repeater control in Asp.Net, What are the uses of Reater control and How to bind data to Repeater control in Asp.Net.

Description

What is Repeater Control
The Repeater control is used to display a repeated list of items that are bound to the control. The Repeater control may be bound to a database table, an XML file, or another list of items.

Repeater is Data Bind Control. Data bind Controls are container controls. Data binding is the process of creating a link between the data source and the presentation UI to display the data. Asp.Net provides rich and wide variety of controls, which can be bound to the data.

Uses of Repeater Control
We know that Repeater Controls are used to display the list if itemsthat bound to the controland it's same as GridView and DataGridView. Repeater control is lightweight and faster to display data when compared with GridView and DataGrid. We can display data in Custom format but it's not poissible in GridView or DataGridView controls and it's doesn't support for pagging and sorting.

Repeater Control have 5 entity which you can say templates.
1. Header Template  2. Item Template  3. AlternatingItemTemplate  4. Seperator Template  5. Footer Template

1. ItemTemplate: ItemTemplate defines how the each item is rendered from data source.

2. AlternatingItemTemplate: AlternatingItemTemplates is used to change the styles of Alternate Items in Data Source collection

3. Header Template: Header Template is used for Header text for Data Source collection and apply styles for header text.

4. Footer Template: Footer Template is used to display footer element for Data Source collection.

5. SeparatorTemplate: SeparatorTemplate will determine separator element which separates each Item in Item collection.

Some points about Repeater Control
  • It is used to display back-end result set. It is used to display multiple tuple.
  • It is unformatted control. The Repeater control is a basic templated data-bound list. It has no built-in layout or styles, so you must explicitly declare all lay out, formatting. and style tags within the control's templates.
  • The Repeater control is the only Web control that allows you to split markup tags across the templates. to create a table using templates, include the begin table tag (<table>) in the Header Template, a single table row tag (<tr>) in the Item template, and the end table tag (</table>) in the Footer Template.
  • The Repeater control has no built-in selection capabilities or editing support. You can use the ItemCommand event to process events that are raised from the templates to the control.

Repeater Control Example in Asp.Net using C#
Before we start to explain, how to use Repeater control and bind data to the control. You neew a table in database. Here I have a table Products_Dts. We bind the product details in repeater control. You can use the following SQL query shown below to create a sample table.

    CREATE TABLE [dbo].[Products_Dts](
      [ProductId] [int] IDENTITY(1,1) NOT
NULL,
      [ProductName] [varchar](50)
NULL,
      [BrandName] [varchar](50)
NULL,
      [Description] [varchar](max)
NULL,
      [launchDate] [varchar](20) NULL,
      CONSTRAINT [PK_Poducts_Dts] PRIMARY KEY CLUSTERED
      (
        [ProductId] ASC
      )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  =
OFF, IGNORE_DUP_KEY = OFF 
      ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    )
ON [PRIMARY]

After creating table in your database. Open your Visual Studio and create a new website. After that add new item and select a WebForm and save it. Now we design .aspx page with three TextBox, button and a Repeater controls. The HTML markup of the .aspx page is as shown below



In the above code, you can see that, I am using div with cdClearFix class. After this, divide this into two div panels, Left & Right with cdLeft & cdRight classes. So include the followin g style as shown below, inside the head tag (<html></html>).

    <style type="text/css">
        .cdClearFix {
            zoom: 1;
        }
        .cdClearFix:after {
            clear: both;
            content: ".";
            display: block;

            visibility: hidden;
        }
        .cdLeft {
            float: left;
        }
        .cdRight {
            float: right;
        }
    </style>

In the left div, I have use TextBox, and Button controls for inserting the product details into the database. And in right div, use Repeater control for bind data from database.

The Repeater control, is the only Web control that allows you to split markup tags across the templates. For this here I am using Table structure inside the Repeater control for displaying the data. Here I include the begin table tag (<table>) in the Header Template, a multiple table row tag (<tr>) in the Item template, and the end table tag (</table>) in the Footer Template.

After completion of .aspx page design, add the following namespaces in code behind page.

   using System;
   using System.Data;
   using System.Data.SqlClient;
   using System.Configuration;

System
namespaces contains many commonly-used types. These are separate from the language-level aliases in the C# language. System can be referenced with different syntax forms. It is usually specified with a using System directive.

System.Data namespaces contain classes for accessing and managing data from multiple data source.

System.Data.SqlClient namespace is the.NET Framework Data Provider for SQL Server. The.NET Framework Data Provider for SQL Server describes a collection of classes used to access a SQL Server database in the managed space.

System.Configuration namespace contains the classes that are used to programmatically access .NET Framework configuration files. It also supplies objects for providing custom section handlers and handling errors in configuration files.

After add the above namespaces, write the following code, shown below, in code behind page for insert an product details at button OnClick event and bind data to the repeater control at page load event.



And write or add the connection string in your web.config file and Change the connection string according to your database and data source. The connection string would be like

    <connectionStrings>
       <add name="ConnectionString" connectionString="Data Source=SHEKHAR-PC;Initial
                 Catalog=TEMP;Persist Security Info=True;User ID=sa;Password=trucom@123"
                 providerName="System.Data.SqlClient" />
   </connectionStrings>

I hope that this article would have helped you in understanding the Repeater Control in Asp.Net....If you like this article, Please share it with others......Thanks!

OutPut



Out
I hope that this article would have helped you in understanding the Repeater Control in Asp.Net. Please share it if you like it.....Thanks!

No comments:

Post a Comment