Repeater control is a light weight control as compared to GridView control. It is not having default functionality as GridView.
In repeater control we have to design our own rows, columns or designing our layouts.
- Create a new ASP.NET Empty Web Site.
Click on File - WebSite - ASP.NET Empty Web Site.
- By default only one file created in project that is Web.Config.
- Right click on project, Add, then Add a New Item,
Select WebForm and give name “Repeater.aspx”
- Select ToolBox’s Data group and then Repeater control. Drag and drop on Repeater page.
- Basic structure of repeater control,
- <asp:Repeater ID="rp" runat="server">
Your code will come here,
- Before starting coding of repeater layouts of columns and rows first create table structure in your database as given below.
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- SET ANSI_PADDING ON
- GO
- CREATE TABLE[dbo].[tblFriends]
- (
- [FriendID][int] IDENTITY(1, 1) NOT NULL, [FriendName][varchar](50) NULL, [Place][varchar](25) NULL, [Mobile][varchar](15) NULL, [EmailAddress][varchar](150) NULL
- )
- ON[PRIMARY]
-
- GO
- SET ANSI_PADDING OFF
- Now, switch back to Repeater.aspx page, we started coding of ASPX page.
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeater.aspx.cs" Inherits="Repeater" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
-
- <asp:Repeater ID="rptFriend" runat="server">
- <ItemTemplate>
- <table>
- <tr>
- <td><b>Friend ID :</b>
- <asp:Label ID="lblFriendID" runat="server" Text='<%# Eval("FriendID")%>'></asp:Label>
- <br />
- <b>Friend Name :</b>
- <asp:Label ID="lblFriendName" runat="server" Text='<%# Eval("FriendName") %>'></asp:Label>
- <br />
- <b>Place :</b>
- <asp:Label ID="lblPlace" runat="server" Text='<%# Eval("Place") %>'></asp:Label>
- <br />
- <b>Mobile :</b>
- <asp:Label ID="lblMobile" runat="server" Text='<%# Eval("Mobile") %>'></asp:Label>
- <br />
- <b>Email Address:</b>
- <asp:Label ID="lblEmailAddress" runat="server" Text='<%# Eval("EmailAddress") %>'></asp:Label>
- </td>
- </tr>
- </table>
- </ItemTemplate>
- <SeparatorTemplate>
- <hr/>
- </SeparatorTemplate>
- </asp:Repeater>
-
- </div>
- </form>
- </body>
-
- </html>
- In design view ASPX will look like the following:
- Right click on project Add Reference, Assemblies, then Framework,
Select System.Configuration.dll
- Add following namespace in REPEATER.ASPX.CS
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
Using System.Data: The namespace to access ADO.NET full functionalities.
Using System.Data.SqlClient: The namespace to access SQL Server database.
Using System.Configuration: To fetch connection string from web.config.
- Now, switch to Repeater.aspx.cs page, we starting coding of code behind file.
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class Repeater: System.Web.UI.Page
- {
-
- string ConStr = ConfigurationManager.ConnectionStrings["MemberCDACConnectionString"].ConnectionString;
-
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindRepeater();
- }
- }
-
-
-
-
- public void BindRepeater()
- {
-
- SqlConnection con = new SqlConnection(ConStr);
- SqlDataAdapter da = new SqlDataAdapter("Select * From tblFriends", ConStr);
- DataSet ds = new DataSet();
- da.Fill(ds, "FriendTable");
- rptFriend.DataSource = ds;
- rptFriend.DataBind();
- }
- }
- Repeater view
In the next article we will learn two main events of REPEATER,
- ItemCommand
- ItemDataBound.
Please, feel free to contact for any query and keep in check for next article.