SIGN UP MEMBER LOGIN:    
ARTICLE

Display Records Horizontally in a Repeater Control

Posted by Satyapriya Nayak Articles | ASP.NET Programming February 15, 2012
This article shows how to show records horizontally in a repeater control.
Reader Level:
 

Introduction

This article shows how to show records horizontally in a repeater control. We will use one repeater control, a label and two buttons named previous and next for paging purposes. Here we will first add items to an array list. Paging facilities are provided by using the PagedDataSource class.

Code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Horizontal_display_repeater._Default" %>
<!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>Untitled Page</title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
    <asp:Repeater id="Repeater1" runat="server"
            onitemdatabound="Repeater1_ItemDataBound">
        <HeaderTemplate >
            <table border="1" width="500px">
        </HeaderTemplate>
        <ItemTemplate>
                <asp:Literal ID="litRowStart" runat="server"></asp:Literal>
                <td><%# Eval("Name") %></td>
                <asp:Literal ID="litRowEnd" runat="server"></asp:Literal>
        </ItemTemplate>
        <FooterTemplate >
            </table>
        </FooterTemplate>
    </asp:Repeater>
    <table width="100%" border="0">
      <tr>
         <td>  <asp:label id="lbl1" runat="server" BackColor="Yellow" BorderColor="Yellow" }
                 Font-Bold="True" ForeColor="#FF3300"></asp:label></td>
      </tr>
      <tr>
       <td> 
           <asp:button id="btn_Previous" runat="server" text="Previous" Width="60px"
               onclick="btn_Previous_Click"></asp:button>
             <asp:button id="btn_Next" runat="server" text="Next" Width="60px"
               onclick="btn_Next_Click"></asp:button></td>
      </tr>
   </table>
    </div>
    </form>
</body>
</
html>

Code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Horizontal_display_repeater
{
    public partial class _Default : System.Web.UI.Page
    {
        private long lCurrentRecord = 0;
        private long lRecordsPerRow = 3;
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindrepeater();
            }
        }
        private void bindrepeater()
        {
            ArrayList mylist = new ArrayList();
            mylist.Add(new Data("Divid"));
            mylist.Add(new Data("Jenifer"));
            mylist.Add(new Data("Andy"));
            mylist.Add(new Data("Soah"));
            mylist.Add(new Data("Landi"));
            mylist.Add(new Data("Adam"));
            mylist.Add(new Data("Raj"));
            mylist.Add(new Data("Ravi"));
            mylist.Add(new Data("Rahul"));
            mylist.Add(new Data("Sunny"));
            mylist.Add(new Data("Harry"));
            mylist.Add(new Data("Ian"));
            PagedDataSource Pds1 = new PagedDataSource();
            Pds1.DataSource = mylist;
            Pds1.AllowPaging = true;
            Pds1.PageSize = 3;
            Pds1.CurrentPageIndex = CurrentPage;
            lbl1.Text = "Showing Page: " + (CurrentPage + 1).ToString() + " of " + Pds1.PageCount.ToString();
            btn_Previous.Enabled = !Pds1.IsFirstPage;
            btn_Next.Enabled = !Pds1.IsLastPage;
            Repeater1.DataSource = Pds1;|
            Repeater1.DataBind();
        }
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Header & e.Item.ItemType != ListItemType.Footer)
            {
                for (int i = 0; i <= e.Item.Controls.Count - 1; i++)
                {
                    System.Web.UI.WebControls.Literal obLiteral = e.Item.Controls[i] as System.Web.UI.WebControls.Literal;
                    if (obLiteral != null)
                    {
                        if (obLiteral.ID == "litRowStart")
                        {
                            lCurrentRecord += 1;
                            if ((lCurrentRecord == 1))
                            {
                                obLiteral.Text = "<tr>";
                            }
                            break;
                        }
                    }
                }
                for (int i = 0; i <= e.Item.Controls.Count - 1; i++)
                {
                    System.Web.UI.WebControls.Literal obLiteral = e.Item.Controls[i] as System.Web.UI.WebControls.Literal;
                    if (obLiteral != null)
                    {
                        if (obLiteral.ID == "litRowEnd")
                        {
                            if (lCurrentRecord % lRecordsPerRow == 0)
                            {
                                obLiteral.Text = "</tr>";
                                lCurrentRecord = 0;
                            }
                            break;
                        }
                    }
                }
            }
        }
        public int CurrentPage
        {
            get
            {
                object s1 = this.ViewState["CurrentPage"];
                if (s1 == null)
                {
                    return 0;
                }
                else
                {
                    return Convert.ToInt32(s1);
                }
            }
            set { this.ViewState["CurrentPage"] = value; }
        }
        protected void btn_Previous_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            bindrepeater();
        }
        protected void btn_Next_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            bindrepeater();
        }
    }
    public class Data
    {
        private string name;
        public Data(string name)
        {
            this.name = name;
        }
        public string Name
        {
            get
            {
                return name;
            }
        }
    }
}

Output

sat1.gif

sat2`.gif

sat3.gif

sat4.gif

Thanks for reading 

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks all

Posted by Satyapriya Nayak Feb 16, 2012

Thank you so very much for this article. I was searching this stuff.

Posted by Jessica Stephen Feb 15, 2012

Hi Satyapriya. Its a very good and useful article.

Posted by Adora Krause Feb 15, 2012

Hi, thanks for this article , keep it up.

Posted by Nitin Singh Feb 15, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor