How to display number of records in repeater web part

   —   
This article shows example how to display number of records of repeater
You will need to create copy of repeater web part. Into this copy please add asp:Literal control with ID="ltlRepeaterCount". Open the code-behind file of copied web part and change following lines:

    public override void OnContentLoaded()
    {
        base.OnContentLoaded();
        SetupControl();
    }


to:

    public override void OnContentLoaded()
    {
        base.OnContentLoaded();
        SetupControl();
        this.repItems.DataBinding += new EventHandler(repItems_DataBinding);
    }

    void repItems_DataBinding(object sender, EventArgs e)
    {
        if (!CMS.GlobalHelper.DataHelper.DataSourceIsEmpty(repItems.DataSource))
        {
            System.Data.DataView dv = repItems.DataSource as System.Data.DataView;
            if ((dv != null) && (dv.Table != null))
            {
                ltlRepeaterCount.Text = dv.Table.Rows.Count.ToString();
            }
        }
    }


Please note:

If you are using ASPX templates, data reload is needed to display the content correctly. Please add the marked line to OnContentLoaded method:

public override void OnContentLoaded()
{
base.OnContentLoaded();
SetupControl();
this.repItems.DataBinding += new EventHandler(repItems_DataBinding);
repItems.DataBind();
}


See also:

Applies to: Kentico CMS 4.0. May work in previous versions as well.
Share this article on   LinkedIn