+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Modifying MetaData in NewsView Details page

Modifying MetaData in NewsView Details page

5 posts, 0 answered
  1. KMac
    KMac avatar
    133 posts
    Registered:
    15 Dec 2008
    02 Jun 2011
    Link to this post
    Hello,

    I'm having a bit of trouble accessing custom metadata in a custom control that inherits from a NewsView.

    I'm using SetItemMetaData to access and change the Metafields in the Master template but keep getting an error when trying to do the same thing for the Details template. It seems like ItemContainer is referring to a repeater that, of course, isn't part of the details page. Is there something I'm missing?

    The SetItemMetaData is below:

    protected override void SetItemMetadata(System.Web.UI.Control itemContainer, Telerik.Cms.Engine.IContent contentItem)
    {
        base.SetItemMetadata(itemContainer, contentItem);
            HyperLink lawyer = (HyperLink)itemContainer.FindControl("lawyer"); 
            string lawyerLink = contentItem.GetMetaData("Lawyer").ToString().Replace(" ", "_");
     
            lawyer.NavigateUrl = "~/who-we-are/" + lawyerLink;
            lawyer.Text = contentItem.GetMetaData("Lawyer").ToString();
    }

    The list template is fairly straightforward as well:
    <telerik:CssFileLink ID="CssFileLink1" EmbeddedFileName="Telerik.News.Resources.ControlTemplates.Frontend.newsCommonLayout.css" FileName="" Media="screen" runat="server" />
    <telerik:JsFileLink id="jsLink" runat="server" ScriptType="jQuery" />
          
    <div class="cf_singleNews">
        <h3>
            <asp:Literal ID="Title" runat="server"></asp:Literal>
        </h3>
        <p>
    <asp:Hyperlink ID="lawyer" runat="server"></asp:Hyperlink> </p>
       
        <p>
            <asp:Literal ID="content" runat="server"></asp:Literal>
        </p>
       
      
        <div align="center"><asp:HyperLink ID="backToList1" Text="<%$Resources:AllNews %>" CssClass="sf_back" runat="server"></asp:HyperLink></div>
    </div>

    The problem is that lawyerlink is always null on the list template but is fine on the master template. Do we use something different to make changes to metadata on a list template?

    Thanks,
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    03 Jun 2011
    Link to this post
    Hello KMac,

    Try overriding SetSingleItemContent(IContent item) where you have access to the object that is loaded in single mode.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. KMac
    KMac avatar
    133 posts
    Registered:
    15 Dec 2008
    03 Jun 2011
    Link to this post
    Thanks Ivan,

    Is there a code sample somewhere for working with SetSingleItemContent. I'm not sure how I reference a literal control that is on the details template. With the SetItemMetaData, it's easy since we have the itemContainer. How to get access to the controls on the details template, though.

    Also, SetItemMetaData seems to run for both the master and the details, how to I identify that I'm in the details mode when in SetItemMetaData so I can avoid some of our custom code from running in the details page?

    Thanks
  4. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    07 Jun 2011
    Link to this post
    This is what I generally do:

    Detect whether you're on the list view:

    if (itemContainer.GetType() == typeof(System.Web.UI.WebControls.RepeaterItem)) 
    {
        // a repeating item template
    }
    else
    {
        // a detail template
    }



    And then, whenever I try to find a control, I make sure to test whether or not I successfully obtained a reference for it:

    HyperLink downloadLink = itemContainer.FindControl("DownloadLink") as HyperLink;
    if (downloadLink != null)
    {
        // code goes here
    }

    Also, if you have templates with naming containers that make FindControl miss the item you're looking for, you can create a recursive version.  Here's one I use.  I put this in a Utility class in App_Code (along with all my other commonly used functions, such as code to detect when in Admin mode, etc):

    namespace CPi.Web.Public
    {
      
        /// <summary>
        /// Summary description for Utility
        /// </summary>
        public class Utility
        {
            /// <summary>
            /// Recurses through all child controls in the root control to find the control specified by id
            /// </summary>
            /// <param name="root">The root of the control hierarchy to recurse</param>
            /// <param name="id">String value of the ID to find.</param>
            /// <returns></returns>
            public static Control FindControlRecursive(Control root, string id)
            {
                if (root.ID == id)
                {
                    return root;
                }
      
                foreach (Control c in root.Controls)
                {
                    Control t = FindControlRecursive(c, id);
                    if (t != null)
                    {
                        return t;
                    }
                }
      
                return null;
            }
          }
    }

    I call this function in my template like this (don't forget to add the "using" reference to your Utility class at the top of your class file):

    Literal websiteLiteral = Utility.FindControlRecursive(itemContainer, "WebsiteLink") as Literal;

    Regards,
    Mike Sharp



  5. KMac
    KMac avatar
    133 posts
    Registered:
    15 Dec 2008
    09 Jun 2011
    Link to this post
    Thanks Mike,

    The if (itemContainer.GetType() == typeof(System.Web.UI.WebControls.RepeaterItem))line did the trick. For some reason I never thought of that before.

    Much appreciated.
Register for webinar
5 posts, 0 answered