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
Thanks for reading
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: