Portal Engine Questions on portal engine and web parts.
Version 4.x > Portal Engine > First (and last) item in a Transformation View modes: 
User avatar
Member
Member
m.rutter - 11/26/2009 4:48:04 AM
   
First (and last) item in a Transformation
Hello everybody.

I'm writing custom transformations for CMS.Article document type. Does exists a simple way to check if the article is the first (or the last) during the transformation?

I thinking to something like this (in the transformation - used within an ArticleList web part):
IF [Article Is The First]
Write This Html-For-First
ELSEIF [Article Is The Last]
Write This Html-For-Last
ELSE
Write This Html-For-MiddleItem

Thank you.

User avatar
Member
Member
zhaojicheng-artmtech - 11/29/2009 7:43:26 AM
   
RE:First (and last) item in a Transformation
First:
int currentIndex=DataItemIndex+1;
if(currentIndex==1)
{
//the first
}

User avatar
Member
Member
zhaojicheng-artmtech - 11/29/2009 8:03:29 AM
   
RE:First (and last) item in a Transformation
All code:
<% 
int page=((PagedDataSource)(((CMS.Controls.CMSRepeater)this.Parent.Parent).DataSource)).CurrentPageIndex;
int pageSize=((CMS.Controls.DataPager)(((CMS.Controls.CMSRepeater)this.Parent.Parent).PagerControl)).PageSize;
int currentIndex=DataItemIndex+1;
if(currentIndex==1)
{
//first
}
if(currentIndex+page*pageSize==count || currentIndex==pageSize)
{
//last
}
%>

User avatar
Member
Member
zhaojicheng-artmtech - 11/29/2009 9:17:26 AM
   
RE:First (and last) item in a Transformation
sorry for the miss the count
int  count=((PagedDataSource)(((CMS.Controls.CMSRepeater)this.Parent.Parent).DataSource)).DataSourceCount; 

User avatar
Member
Member
m.rutter - 11/30/2009 4:11:05 AM
   
RE:First (and last) item in a Transformation
Hi zhaojicheng, thank you very much for your tips! I will try asap.

Kind regards,
Marcello

User avatar
Member
Member
zhaojicheng-hichina - 12/5/2009 11:14:16 PM
   
RE:First (and last) item in a Transformation
Hi you can add my msn account "zjc_8112282hotmail.com" if you wish.I am happy to help you.

User avatar
Member
Member
zhaojicheng-hichina - 12/5/2009 11:14:48 PM
   
RE:First (and last) item in a Transformation
zjc_8112282@hotmail.com

User avatar
Member
Member
zhaojicheng-hichina - 12/5/2009 11:17:24 PM
   
RE:First (and last) item in a Transformation
zjc_811228@hotmail.com

User avatar
Member
Member
sales-bluweb.co - 2/6/2010 5:43:10 AM
   
RE:First (and last) item in a Transformation
Hi I'm wondering if you can help me. I've taken a look at this thread but haven't managed to get anywhere. I'm using the news document type. I have a page called News.aspx which uses the CMSRepeater to display news articles and also facilitates the selecteditemtransformation property to display a selected news article.

I want Next and Back buttons when a news article has been selected (currently in it's selected item transformation view). I thought if I could get the next and previous itemindex using the current dataitemindex this would be easy to do. I tried the code on this thread but received an error saying I can't cast a DataView to a PagedDataSource.
int page = ((PagedDataSource)news.DataSource).DataSourceCount;

Do you think you might be able to help me? Does this make sense? Am I going the right way to implement the functionality I require?

Thanks very much in advance.
Tom.

User avatar
Member
Member
zhaojicheng-hichina - 2/9/2010 6:19:26 AM
   
RE:First (and last) item in a Transformation
yes,i can do that.please give me 2 days. and after 2 days i will giive you some code

User avatar
Member
Member
sales-bluweb.co - 2/9/2010 1:27:36 PM
   
RE:First (and last) item in a Transformation
genius! - thank you very very much! I really appreciate it!

User avatar
Member
Member
zhaojicheng-hichina - 2/9/2010 9:10:01 PM
   
RE:First (and last) item in a Transformation
define custom function
    private static TreeNode NextDocument(object documentId)
{

TreeProvider provider = new TreeProvider();

object obj = provider.Connection.ExecuteScalar(string.Format("select top 1 * from cms_document where documentid>{0} order by documentid desc", ValidationHelper.GetInteger(documentId, 0)),
null,
QueryTypeEnum.SQLQuery, false);
if (obj != null)
{
int nextDocumentId = (int)obj;
return provider.SelectSingleDocument(nextDocumentId);
}
return null;
}

public static string NextAliasPath(object documentId)
{
TreeNode nextTreeNode = NextDocument(documentId);
if (nextTreeNode != null)
{
return (string)nextTreeNode.GetValue("NodeAliasPath");
}

return "";
}

public static string NextDocumentUrl(object documentId)
{

TreeNode nextTreeNode = NextDocument(documentId);
if (nextTreeNode != null)
{
return (string)nextTreeNode.GetValue("DocumentUrlPath");
}

return "";
}



private static TreeNode PrevDocument(object documentId)
{

TreeProvider provider = new TreeProvider();
object obj = provider.Connection.ExecuteScalar(string.Format("select top 1 * from cms_document where documentid<{0} order by documentid asc",ValidationHelper.GetInteger(documentId, 0)),
null,
QueryTypeEnum.SQLQuery, false);
if (obj != null)
{
int prevId = (int)obj;
return provider.SelectSingleDocument(prevId);
}
return null;
}

User avatar
Member
Member
zhaojicheng-hichina - 2/10/2010 1:49:16 AM
   
RE:First (and last) item in a Transformation
any question please contact me with my msn:
kentico-support@hotmail.com

User avatar
Member
Member
zhaojicheng-hichina - 2/10/2010 1:49:44 AM
   
RE:First (and last) item in a Transformation
please remark:kentico

User avatar
Member
Member
sales-bluweb.co - 2/10/2010 1:34:13 PM
   
RE:First (and last) item in a Transformation
Hi friend.

I've added you to msn. There are a couple of errors relating to" does not contain a definition for provider.Connection.ExecuteScalar()". I'm in kentico v4 if that's anything to do with it? Sorry maybe I should have mentioned that.

User avatar
Member
Member
zhaojicheng-hichina - 2/11/2010 12:07:43 AM
   
RE:First (and last) item in a Transformation
my kenticocms version is 4.1

User avatar
Member
Member
zhaojicheng-hichina - 2/11/2010 3:45:36 AM
   
RE:First (and last) item in a Transformation
as i know "Connection.ExecuteScalar" is already exists with version 4

User avatar
Member
Member
sales-bluweb.co - 2/11/2010 3:56:45 PM
   
RE:First (and last) item in a Transformation
the ExecuteScalar wasn't there. Thanks very much for your help though as it pointed me in the right direction and I've managed to do what I wanted to do. Thanks!!!!

For anyone else needing the code here's mine:-
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using CMS.CMSHelper;
using CMS.UIControls;
using CMS.TreeEngine;
using CMS.IDataConnectionLibrary;
using CMS.GlobalHelper;

/// <summary>
/// News Page.
/// Author: Tom Miller.
/// </summary>
public partial class CMSTemplates_BringOnRetirement_NewsPage : TemplatePage
{
protected void Page_Load(object sender, EventArgs e)
{

if (CMSContext.CurrentDocument.DocumentName != "News")
{
int thisDocumentID = CMS.CMSHelper.CMSContext.CurrentDocument.DocumentID;
string nextDocumentAliasPath = string.Empty;
nextDocumentAliasPath = NextDocumentAliasPath(thisDocumentID);
if (!(string.IsNullOrEmpty(nextDocumentAliasPath)))
{
buttonNext.Visible = true;
ViewState["NextDocumentAliasPath"] = nextDocumentAliasPath;
}
string previousDocumentAliasPath = string.Empty;
previousDocumentAliasPath = PreviousDocumentAliasPath(thisDocumentID);
if (!(string.IsNullOrEmpty(previousDocumentAliasPath)))
{
buttonPrevious.Visible = true;
ViewState["PreviousDocumentAliasPath"] = previousDocumentAliasPath;
}
}
else
{
buttonNext.Visible = false;
buttonPrevious.Visible = false;
}

buttonNext.Click += new EventHandler(buttonNext_Click);
buttonPrevious.Click += new EventHandler(buttonPrevious_Click);
}

protected void buttonNext_Click(object sender, EventArgs e)
{
string nextDocumentAliasPath = ViewState["NextDocumentAliasPath"].ToString();
if (!(String.IsNullOrEmpty(nextDocumentAliasPath)))
{
Response.Redirect("~" + nextDocumentAliasPath + ".aspx");
}
}

protected void buttonPrevious_Click(object sender, EventArgs e)
{
string previousDocumentAliasPath = ViewState["PreviousDocumentAliasPath"].ToString();
if (!(String.IsNullOrEmpty(previousDocumentAliasPath)))
{
Response.Redirect("~" + previousDocumentAliasPath + ".aspx");
}
}

private string NextDocumentAliasPath(int documentId)
{
TreeProvider provider = new TreeProvider();
DataSet ds = provider.Connection.ExecuteQuery
(string.Format("select top 1 * from View_CONTENT_news_Joined where DocumentID<{0} order by documentid desc",
ValidationHelper.GetInteger(documentId, 0)),
null, QueryTypeEnum.SQLQuery,
false);

if (ds.Tables[0].Rows.Count > 0)
{
string nextDocumentAliasPath = ds.Tables[0].Rows[0]["NodeAliasPath"].ToString();
return nextDocumentAliasPath;
}
return string.Empty;
}

private static string PreviousDocumentAliasPath(int documentId)
{
TreeProvider provider = new TreeProvider();
DataSet ds = provider.Connection.ExecuteQuery
(string.Format("select top 1 * from View_CONTENT_news_Joined where DocumentID>{0} order by documentid",
ValidationHelper.GetInteger(documentId, 0)),
null, QueryTypeEnum.SQLQuery,
false);

if (ds.Tables[0].Rows.Count > 0)
{
string previousDocumentAliasPath = ds.Tables[0].Rows[0]["NodeAliasPath"].ToString();
return previousDocumentAliasPath;
}
return string.Empty;
}
}

User avatar
Member
Member
richard.pendergast-notatallstrange - 3/3/2010 11:20:24 PM
   
RE:First (and last) item in a Transformation
Thought I might mention since this post is still getting a lot of attention, that there is a very simple answer to the initial question posted:
<%# DataItemIndex ==0?”First record”:”Not first record” %>

<%# DataItemIndex==DataRowView.DataView.Count-1?”Last record”:”Not last record” %>

This does not answer the question posted by sales-bluweb.co, which zhaojicheng-hichina answered, but might provide a starting point for others.

You can read more (posted by Matthew Lee) at http://www.kenticodeveloper.com/Matthew-Lee/September-2009/Conditional-transformations-in-Kentico.aspx

User avatar
Member
Member
lfeuer-bluemodus - 12/21/2010 4:41:01 PM
   
RE:First (and last) item in a Transformation
I'm trying to get an id on both my first li and last li for current (ie: firstcurrent & lastcurrent). i am able to get the first li working, but cannot get the last one working as well. below is my current code:
<script runat=server>
string isCurrent(string currentURL) {
Uri MyUrl = Request.Url;
if (MyUrl.AbsoluteUri.ToString().Replace("%2f","/").IndexOf(currentURL) > 1) {
return "firstcurrent";
} else {}return "";}
</script>

<script runat=server>
string isCurrent2(string currentURL) {
Uri MyUrl = Request.Url;
if (MyUrl.AbsoluteUri.ToString().Replace("%2f","/").IndexOf(currentURL) > 1) {
return "lastcurrent";
} else {}return "";}
</script>

<li id="<%# isCurrent(GetDocumentUrl()) %>""<%# isCurrent2(GetDocumentUrl()) %>" class="<%# DataItemIndex==0?"first":"last" %>">
<a href="<%#GetDocumentUrl()%>"><span><%#IfEmpty(Eval("DocumentMenuCaption"), Eval("DocumentName"), Eval("DocumentMenuCaption"))%></span></a>
</li>

can anyone help?? thanks in advance!

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 12/31/2010 7:45:06 AM
   
RE:First (and last) item in a Transformation
Hi,

could you please specify why both functions have the same comparison code or optionally describe your aim in more details (e.g. result example) if I'm missing something..?

Thanks and regards
Zdenek

User avatar
Member
Member
lfeuer-bluemodus - 12/31/2010 2:00:19 PM
   
RE:First (and last) item in a Transformation
I have a menu, I have the cufon set to Hover and Active set to bold. When you refresh the page, the active state works as the bold. When you Hover over any items, they don't bold and the current Active bolded menu item removes the bold. Does this make sense?

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 1/3/2011 11:11:28 AM
   
RE:First (and last) item in a Transformation
If I'm getting it right, the only problem is the last item...
the last has to be compared with the number of displayed records... that can be obtained from the current DataSet...

So, if you're displaying it with Repeater, you can find it using FindControl or better by loop through controls until you find the parent control of Repeater's type. The repeater's DataSource property contains the needed DataSet.
Hope this helps.

Regards,
Zdenek

User avatar
Certified Developer v7
Certified  Developer v7
Nathoushka - 1/12/2011 1:20:13 PM
   
RE:First (and last) item in a Transformation
richard.pendergast-notatallstrange wrote: Thought I might mention since this post is still getting a lot of attention, that there is a very simple answer to the initial question posted:

<%# DataItemIndex ==0?”First record”:”Not first record” %>

<%# DataItemIndex==DataRowView.DataView.Count-1?”Last record”:”Not last record” %>

This does not answer the question posted by sales-bluweb.co, which zhaojicheng-hichina answered, but might provide a starting point for others.

You can read more (posted by Matthew Lee) at http://www.kenticodeveloper.com/Matthew-Lee/September-2009/Conditional-transformations-in-Kentico.aspx

I've tried "DataRowView.DataView.Count" but it returns me a System.NullReferenceException... Am I missing something?

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 1/13/2011 3:19:42 AM
   
RE:First (and last) item in a Transformation
Hi,
I've tried "DataRowView.DataView.Count" but it returns me a System.NullReferenceException... Am I missing something?

Does it mean that you inserted following transformation code into body of transformation selected in Transformation property of repeater webpart?
<%# DataItemIndex==DataRowView.DataView.Count-1?"Last record":"Not last record" %>

Could you please describe in more details how did you use the code? Which version/hotfix of Kentico CMS are you using?

Best regards,
Ivana Tomanickova

1 2