How to find out whether current page is the last page in listing transformation in repeater
This article describe example how to find out whether current page is the last page in "listing" transformation in repeater, especially when built-in paging is enabled. In this particular example we will set different background for last item than for other items.
Please follow procedure bellow:
1) Add custom function for transformation
2) Put following method into MyFunctions.cs file created in step 1:
/// <summary>
/// Returns true if current item in transformation is last item. Otherwise returns false.
/// </summary>
/// <param name="sender">Transformation object from which the function is called</param>
/// <param name="count">Count of items displayed by repeater</param>
/// <param name="index">Index of current item</param>
public static bool IsCurrentItemLast(Control sender, int count, int index)
{
//get the repeater object
Control myParent = sender;
while (myParent != null)
{
myParent = myParent.Parent;
if (myParent is CMS.Controls.CMSRepeater)
{
CMS.Controls.CMSRepeater myRepeater = myParent as CMS.Controls.CMSRepeater;
if ((count - 1) == ((myRepeater.PagerControl.PageSize - 1) * myRepeater.PagerControl.CurrentPage + index))
{
return true;
}
}
}
return false;
}
3) Put following sample code into transformation of CMSRepeater:
<div style="background-color: <%# MyFunctions.IsCurrentItemLast(this, DataRowView.DataView.Count, DataItemIndex)? "#FFFFAA" : "#AAFFFF" %>;">
sample text
</div>
If you don't use paging, you may use following expression instead (you don't need to create custom function in this case):
<div style="background-color: <%# (DataRowView.DataView.Count - 1 == DataItemIndex) ? "#FFFFAA" : "#AAFFFF" %>;">
sample text
</div>
-md-
See also: Adding custom function for transformationApplies to: Kentico CMS 5.5 (Should work in previous versions as well)