PageControl Content

Posted by Community Admin on 04-Aug-2018 16:44

PageControl Content

All Replies

Posted by Community Admin on 27-Jul-2011 00:00

How do I access the content inside of a PageControl?

Here is what I have so far:

var pageManager = PageManager.GetManager();
PageSiteNode currentNode = SiteMapBase.GetActualCurrentNode();
  
if (currentNode == null)
    return;
  
PageData pageData = pageManager.GetPageData(currentNode.PageId);
  
foreach (PageControl webCtrl in pageData.Controls)
    if (webCtrl.PlaceHolder == "ContentPlaceHolder1")
    
        Literal1.Text =  ???
    

Posted by Community Admin on 28-Jul-2011 00:00

I think I figured it out!

foreach (PageControl webCtrl in pageData.Controls)
    if (webCtrl.PlaceHolder == "ContentPlaceHolder1")
    
        var cntr = pageManager.LoadControl(webCtrl) as ContentBlock;
        Literal1.Text = cntr.Html;
    

Posted by Community Admin on 22-Sep-2011 00:00

just wanted to say thanks for posting your solution! I was trying to figure out exactly this, getting a PageControl from a Sitefinity page and converting it to a regular control.

the LoadControl method did the trick! thanks again!

Posted by Community Admin on 08-Mar-2013 00:00

You can also do it without the page manager if you like:

pageData.Controls.FirstOrDefault(x => x.ObjectType == typeof(ContentBlock).FullName && x.PlaceHolder == placeHolder).Properties.FirstOrDefault(x => x.Name == "Html").Value;

Have you ever done this with other controls. Like I want to get the rendered HTML that would be created by a NewsView. I am trying this but always get back an empty string:

var pageControl = pageData.Controls.FirstOrDefault(x => x.ObjectType == typeof(NewsView).FullName && x.PlaceHolder == placeHolder);
var stringBuilder = new StringBuilder();
var htmlTextWriter = new HtmlTextWriter(new StringWriter(stringBuilder));
var newsView = PageManager.GetManager().LoadControl(pageControl);
newsView.RenderControl(htmlTextWriter);
content += stringBuilder.ToString();

Any ideas?

Posted by Community Admin on 24-Oct-2014 00:00

Thank you for posting the solution. This was truly helpful. 

This thread is closed