Gettin' Freaky with Transformations!

1
Comments
5
Votes
Login to vote

I wouldn't recommend writing code large amounts of code within transformations; usually, I try to keep them as simple as possible so that front-end developers find them easy to work with.  However, when you need to, this tip may be helpful.

It's important to remember Asp.net's Page Lifecycle when implementing code within a transformation.  If you pick an event that's too early within the life cycle, you'll probably end up getting a null exception.  When scripting within a transformation, you need to be familiar with which tags to use.  If you try to use:

<%
    int nodeId = Eval(this.DataItem, "NodeID");
%>

you'll get a null exception because the data isn't available.

If you need to do complex coding within a transformation, try this:

<script runat="server"> 
public string nodeId { get; set; } 
protected override void OnDataBinding(EventArgs e) 
{ 
    base.OnDataBinding(e); 
    nodeId = DataBinder.Eval(this.DataItem, "NodeID").ToString(); 
</script> 

Obviously, my example could be accomplished by simply using <%# %> tags, but consider combining this with Ryan Williams', Find the Closest Ancestor Method.

 

<script runat="server"> 
    private int nodeId { get; set; }
    protected CMS.TreeEngine.TreeNode _TreeNode;
    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);
        nodeId = int.Parse(DataBinder.Eval(this.DataItem, "NodeID").ToString());      
    _TreeNode = _Functions.GetClosestAncestorFromCurrent("Custom.EventSeriesInstance", nodeId);
     }
</script>

You now have access to all of the parent node's data throughout your transformation with one call.

 
Posted by Jason Ellison on 11/24/2010 11:48:18 AM
  
Comments
acasal
THANKS SO MUCH! I've been trying to find this solution forever for my repeater menus.
7/18/2012 1:53:02 PM