Find out which content is within a <asp:ContentPlaceHolde

Posted by Community Admin on 04-Aug-2018 15:33

Find out which content is within a

All Replies

Posted by Community Admin on 18-Jun-2013 00:00

Hi,

Very simply, I have multiple content items in a content placeholder as defined on the master page (a user has dragged and dropped a couple content blocks to it through the interface)

I need to find out all the items of content that are within the placeholder <asp:ContentPlaceHolder Id="contentPlaceholderRightHandSide" runat="server"> as defined on the master page. How can I do that?

Essentially if the user has added two content blocks to it, I want to get the two content blocks and also, if possible in the correct order. Help is much appreciated.

Thanks,

Ricardo

Posted by Community Admin on 18-Jun-2013 00:00

Ricardo,

Check out the Adding and removing controls page in the documentation. It shows examples of how to access and manipulate controls.

Posted by Community Admin on 18-Jun-2013 00:00

/// <summary>
       /// Returns a ControlData list from a specific page & place holder
       /// </summary>
       /// <param name="pageTitle">Title of the page you want to retrieve controls from</param>
       /// <param name="placeHolder">Placeholder within page you want to retrieve controls from</param>
       /// <returns></returns>
       public static List<ControlData> GetContentItems(string pageTitle, string placeHolder)
       
           var pageManager = PageManager.GetManager();
           var page = pageManager.GetPageNodes().Where(p => p.Title == pageTitle).SingleOrDefault();
           var controls = page.Page.Controls.Where(c => c.PlaceHolder == placeHolder).ToList<ControlData>();
           return controls;
       

This will return a List of controls. From my testing it seems to retain its order.

Posted by Community Admin on 19-Jun-2013 00:00

Exactly what I needed! Thanks so much Jonathan and Tim.

This thread is closed