Getting the number of repeater items from the repeater webpart

Chidozie Bright asked on January 29, 2021 01:30

I am trying to get the number of items from a repeater so that i ca perform a for-loop however the seemingly obvious way to do this (repItems.Items.Count) always returns 0. Is there a way to do this?

public void CreateCarouselPager()
{
for ( var i=0; i < repItems.Items.Count; i++ )...

Recent Answers


Brenden Kehren answered on January 29, 2021 15:25

Where are you doing this? In code-behind or in a template or transformation? Also what version are you working with?

0 votesVote for this answer Mark as a Correct answer

Chidozie Bright answered on January 29, 2021 23:48 (last edited on January 29, 2021 23:52)

Hi Brandon. To answer your questions I am doing this in the code-behind and currently for K12 Portal Engine. However, after sleeping on it I figured it out. One needs to bind the data to the object in order to access the items. So I just added a repItems.DataBind(); to my method and then called the repItems.Items.Count. As follows for those looking for this answer:

public void CreateCarouselPager()
    {
        repItems.DataBind();

        for (var i=0; i <= repItems.Items.Count; i++)
        {
            string pageCounter = string.Format("{0:D2}", i + 1);

            lblCarouselPagerButtons.Text += "<button type=\"button\" data-nav=\"" + i + "\">" + pageCounter + "</button>" ;
        }
    }
1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.