Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bob Jones 33 posts 54 karma points
    Jan 04, 2012 @ 17:48
    Bob Jones
    0

    Asp:Repeater used to iterate comments as stored in umbraco..

    I am trying to display a list of comments in a thread, and have decided to use an asp:repeater to iterate over the comments in a given thread to display them. I have added properties of 'userName', 'postedDate' and 'bodyText' to the comments document type, which I am trying to pull out and display with a repearter.

    Here is my repeater:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CommentRepeater.ascx.cs" Inherits="newUmb.CommentRepeater" %>
    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
        <h2>Posted by: <%#Eval("userName") %> on <%#Eval("postedDate") %></h2>
        <br />
        <%#Eval("bodyText") %>
        </ItemTemplate>
    
        <HeaderTemplate>
            <div class="comment">
        </HeaderTemplate>
        <FooterTemplate>
            </div>
        </FooterTemplate>
    
    </asp:Repeater>

    And here is my code behind:

    protected void Page_Load(object sender, EventArgs e) {
                Node thread = Node.GetCurrent();
                Repeater1.DataSource = thread.ChildrenAsTable();
                Repeater1.DataBind();
    }

    The error that I get when I try to execute this is '

    System.Data.DataRowView' does not contain a property with the name 'userName'

    I guess this is becuase I can only call generic node types using the eval thingy on the ascs control?

    So can anyone tell me how I would successfully call the node's custom properties from here? I imagine its something like the getProperty("property").Value code but need a hand in the right direction!

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jan 04, 2012 @ 18:04
    Ismail Mayat
    0

    Bob,

    You need to cast to Node then call getProperty 

    <%#((Node)Container.DataItem).getProperty("userName").Value.ToString()%>

    However safer way would be to create a method in code behind with signature like

    protected string GetNodeProperty(Node n, string propertyToGet){

    if(n.getProperty(propertyToGet)!=null){

    return n.getProperty.(propertyToGet).ToString();

    }

    return string.Empty

    }

    Regards

    Ismail

  • Bob Jones 33 posts 54 karma points
    Jan 04, 2012 @ 18:14
    Bob Jones
    0

    Thanks Ismail,

    I used the code like this (the only difference being I have written a full library path to the node, which I'm unsure will affect anything):

     <%#((umbraco.presentation.nodeFactory.Node)Container.DataItem).GetProperty("bodyText").Value.ToString()%>

    But I am getting the following error message now:

    Unable to cast object of type 'System.Data.DataRowView' to type 'umbraco.presentation.nodeFactory.Node'.

    Any ideas?

     

    On a side note: When I high5 someone it appears to have gone whenever I refresh the page?!

     

  • Bob Jones 33 posts 54 karma points
    Jan 04, 2012 @ 18:55
    Bob Jones
    0

    ok I got it working for me in case anyone else ever wants to do similar.

    Taking Ismail's suggestion I came up with this method to put in the code behind:

    protected string GetNodeProperty(int Id, string propToGet) {

    Node n = new Node(Id);

     

    if (n.GetProperty(propToGet) != null) {

    return n.GetProperty(propToGet).Value.ToString();

    }

    return string.Empty;

    }

     

    Which I was then able to call in the repeater like this:

     

     

     

     

    Posted by:on 

     

     

     

     

     

    For some reason I got stuck trying to cast the node to the data row item in the repeater but was able to work around the problem this way..

     

Please Sign in or register to post replies

Write your reply to:

Draft