The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.
In Sitefinity you can programmatically create a page and populate it with controls. In some cases you might need to get a specific layout control in your page template using the set css class attribute. You might need to change the template layout control markup, as well.
We get the template layout controls and then using a html parser we get the control attributes. This is not a fast procedure but it should not be considered to find and change layout controls programmatically very often.
We get the layout controls and process them:
private
static
TemplateDraftControl GetLayoutControl(PageManager manager,
string
@
class
, TemplateDraft draftTemplate)
{
var layoutControls = draftTemplate.Controls;
foreach
(var control
in
layoutControls)
{
var ctrlData = manager.GetControl<TemplateDraftControl>(control.Id);
var layProp = ctrlData.PropertiesLang.Single(p => p.Name ==
"Layout"
);
var layout = layProp.Value;
if
(!layout.StartsWith(
"~/"
) && !layout.EndsWith(
".ascx"
, StringComparison.OrdinalIgnoreCase))
{
var found = TryFindControl(ctrlData, layout, @
class
);
if
(found)
{
return
ctrlData;
}
}
else
{
layout = GetDefaultLayoutControlsHtml(ctrlData, layout);
var found = TryFindControl(ctrlData, layout, @
class
);
if
(found)
{
return
ctrlData;
}
}
}
return
new
TemplateDraftControl();
}
I have created a helper class containing methods for getting the layout control and getting and setting its markup.
Sample usage:
var manager = PageManager.GetManager();
var helper =
new
LayoutControlsHelper();
var mycontrol = helper.GetLayoutControlFromPage(manager,
"MyPage"
,
"customclass"
);
if
(mycontrol.Id != Guid.Empty)
{
var html = helper.GetControlHtml(mycontrol);
// change the html of the layout control
html +=
"<span>This is from debug</span>"
;
helper.ChangeControlHtml(manager, mycontrol, html);
}
The full source code could be downloaded from GitHub here.
Nikola Zagorchev is a Tech Support Engineer at Telerik. He joined the Sitefinity Support team in March 2014.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.