ASPX templates
Version 5.x > ASPX templates > CMSRepeater Item Count View modes: 
User avatar
Member
Member
eric.rovtar-hudsonchapel - 8/9/2010 10:38:02 AM
   
CMSRepeater Item Count
Is there a way to get the items a CMSRepeater is displaying?

I tried using repeater.Items.Count, but this seems to always return 0.

Here's my code:

CMS.Controls.CMSRepeater repeater = new CMS.Controls.CMSRepeater();
repeater.ClassNames = "cms.event";
repeater.TransformationName = "cms.event.thisweek";
repeater.Path = "/{0}/%";
repeater.WhereCondition = String.Format("EventDate between '{0}' AND '{1}'", today.AddDays(i).ToString("yyyy-MM-dd"), today.AddDays(i + 1).ToString("yyyy-MM-dd"));

//if (repeater.Items.Count > 0)
{
plContentText.Controls.Add(dayName);
plContentText.Controls.Add(repeater);
}


Thanks!

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/10/2010 8:31:27 AM
   
RE:CMSRepeater Item Count
Hi,

how to count number of items in CMS repeater is described in this article.

Best regards,
Ivana Tomanickova

User avatar
Member
Member
eric.rovtar-hudsonchapel - 8/13/2010 11:36:07 AM
   
RE:CMSRepeater Item Count
But I'm trying to do this from code behind? How can I do that, if I'm required to use a Web Part?

Which Repeater am I supposed to be copying? CMSRepeater? I can't copy a Control, can I?

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/17/2010 9:02:20 AM
   
RE:CMSRepeater Item Count
Hi,

the easiest way how to count items in repeater is in transformation using following macro:
<%# DataRowView.DataView.Count %>

If you would like to count repater items in code behind of webpart you can do it following way:
1. Create a clone of CMSrepeater webpart and modify a content of cloned webpart as discribed in this article
2. Please add repItems.DataBind(); to public override void OnContentLoaded() method
3. Now in your aspx.cs teplate create a repeater instance:

public partial class CMSTemplates_CorporateSiteAspx_temp : TemplatePage
{
protected void Page_Load(object sender, EventArgs e)
{
// Create an instance of cloned web part
CMSWebParts_Viewers_Documents_cmsrepeateritem repeater = Page.LoadControl("~/CMSWebParts/Viewers/Documents/cmsrepeateritem.ascx") as CMSWebParts_Viewers_Documents_cmsrepeateritem;
repeater.Path="Events/%";
repeater.TransformationName="cms.bookingevent.preview";
repeater.ClassNames="cms.bookingevent";

// Display repater in Panel control
panel.Controls.Add(repeater);
panel.DataBind();

}
}


4. Your aspx file should look like this one:

<%@ Page Title="" Language="C#" MasterPageFile="~/CMSTemplates/CorporateSiteAspx/Root.master" AutoEventWireup="true" CodeFile="temp.aspx.cs" Inherits="CMSTemplates_CorporateSiteAspx_temp" %>

<%@ Register Src="~/CMSWebParts/Viewers/Documents/cmsrepeateritem.ascx" TagName="repeater" TagPrefix="uc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="plcMain" Runat="Server">

<asp:Panel ID="panel" runat="server"></asp:Panel>

</asp:Content>



Another possibility how to display number of repeater items is by changing protected override void OnPreRender(EventArgs e) method to look like following one in code behind of cloned webpart. The other changes in this file are not needed in this case (do not modify public override void OnContentLoaded() and do not create void repItems_DataBinding(object sender, EventArgs e) method):

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);

Visible = !repItems.StopProcessing;

if (DataHelper.DataSourceIsEmpty(repItems.DataSource) && (repItems.HideControlForZeroRows))
{
Visible = false;
ltlRepeaterCount.Visible = false;
}
else
{
ltlRepeaterCount.Text = repItems.Items.Count.ToString();
}
}


This way number of repeater items will be displayed via cloned web part control.

Best regards,
Ivana Tomanickova