[Kentico 8] Retrieve the total number of data items in a transformation

Victor Hugo Garcia asked on May 1, 2014 13:32

Hello friends,

I'm currently coding a transformation, however I found an issue when trying to get the Total number of data items, so I can close some divs at the end. Check the code below:

    <%# (DataItemIndex + 1) % 2 == 1 ? "<div class='contactcols'>" : "" %>
<div class='contactcol<%# (DataItemIndex + 1) % 2 == 1 ? "1" : "2" %>'>
        <span class='contacttitle'>
          <a href='#'>
            <%# Eval("Name") %>
          </a>
        </span>
            <%# Eval("FullAddress") %><br />
            <%# Eval("Phone").ToString().Length > 0 ? "Phone: " + Eval("Phone").ToString() + "<br />" : "" %>
            <%# Eval("Phone").ToString().Length > 0 ? "Fax: " + Eval("Fax").ToString() + "<br />" : "" %>
</div>
<%# (DataItemIndex + 1) % 2 == 1 ? "" : "<br class='clearboth' /></div>" %>
<!-- Hardcoded the limit to close the div due Kentico API bug: DataItemCount is not retrieving the total number of data items -->
<%# (DataItemIndex + 1) == 19 ? "<br class='clearboth' /></div>" : "" %>

When printing: <%# DataItemCount %> I get: 0 and of course I'm 100% it is returning more than that, in fact 19, there is my logic of 19 temporary.

Do you know if this is a bug on Kentico 8? any workaround?

Thanks

Recent Answers


3Seed Marketing answered on May 1, 2014 14:50

For an ASCX transformation try...

<%# DataRowView.DataView.Count %>

1 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 1, 2014 15:45

In v8 you have the transformation methods IsFirst(), IsLast() and IsCurrentDocument(). Very very helpful!

0 votesVote for this answer Mark as a Correct answer

Victor Hugo Garcia answered on May 1, 2014 16:10

Jim MacDonald: I tried that one and it returns: "object not set to an instance of an object" error

Brenden Kehren: I just tried that one, but it always return: True

Additional notes: I'm binding the CMS:repeater from a List

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 2, 2014 11:40

IsFirst() is the equivalent of DataItemIndex == 0. IsLast() is the equivalent of DataItemIndex == DataView.Count - 1. They tell you if its the first item in the instance or the last one in the instance.

When are you binding your repeater? It should be in the within the OnContentLoaded method.

0 votesVote for this answer Mark as a Correct answer

Victor Hugo Garcia answered on May 2, 2014 15:45 (last edited on May 5, 2014 02:56)

Yes, I'm binding the repeater OnContentLoaded() Method.

/// <summary>
/// Content loaded event handler.
/// </summary>
public override void OnContentLoaded()
{
    base.OnContentLoaded();
    SetupControl();
}

/// <summary>
/// Initializes the control properties.
/// </summary>
protected void SetupControl()
{
 // Setup the control
        bool hideControlForZeroRows = true;
        // Public
        basicRepeater.HideControlForZeroRows = hideControlForZeroRows;
        basicRepeater.ZeroRowsText = "No data found";
        basicRepeater.DataBindByDefault = true;
        basicRepeater.DataSource = ListsLogic.GetItems(Id);
        if (!DataHelper.DataSourceIsEmpty(basicRepeater.DataSource))
        {
            if ((transformationName != null) && (transformationName.Trim() != ""))
            {
                // Get url to transformation and load it
                basicRepeater.ItemTemplate = CMSDataProperties.LoadTransformation(basicRepeater, transformationName);
            }
        }
        else
        {
            if (hideControlForZeroRows)
            {
                Visible = false;
            }
        }
  }
0 votesVote for this answer Mark as a Correct answer

Victor Hugo Garcia answered on May 2, 2014 15:46

Sorry for the bad format in the previous reply, but this editor is not friendly at all.

0 votesVote for this answer Mark as a Correct answer

Victor Hugo Garcia answered on May 9, 2014 08:24 (last edited on May 9, 2014 08:24)

well, in the meantime, instead of using divs or tables, I used lists li, so we can avoid this.

anyways if somebody has a solution is always welcome.

Thanks

0 votesVote for this answer Mark as a Correct answer

Alex Johnson answered on June 2, 2015 15:20

I'm seeing the same error where DataItemCount always returns 0 and IsLast() always returns true only for a repeater in which I am using a Macro Data Source which returns an array of objects. Is this just a bug in Kentico?

Thanks

0 votesVote for this answer Mark as a Correct answer

Christian Kuczkowski answered on June 11, 2015 14:35

I ran into this myself as well, with a Hierarchical Viewer that was pulling FAQ page types nested inside of folders. What I was able to do in the transformation was reference the parent folder & count the FAQ children. The reason I took this approach was that I wasn't looking for the last child, but rather, a ratio to break up the content (e.g. 1/2, 1/3 etc). I also used NodeOrder instead of DataItemIndex because of the hierarchy.

{%
      if(Documents[NodeAliasPath].NodeOrder = Ceiling((Documents[NodeAliasPath].Parent.Children.Count/3)) ) {
        //code here to close a div & start a new one
      }
#%}
0 votesVote for this answer Mark as a Correct answer

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