PageControl Content
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 = ???
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;
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!
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?
Thank you for posting the solution. This was truly helpful.