In this section we are going to demonstrate how to programmatically remove controls from a page.
Deleting all controls in a particular Container of a page:
| RemoveControl |
Copy Code |
|
// create a new instance of CmsManager Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager(); // find the sitemap node of the current
page Telerik.Cms.Web.CmsSiteMapNode currentNode =
(Telerik.Cms.Web.CmsSiteMapNode)SiteMap.CurrentNode; // get the CmsPage associated with the current
node Telerik.Cms.ICmsPage currentPage = currentNode.CmsPage; // we are going to use
Controls collection of a staged version of a page to
// access all controls of current page IList<Telerik.Cms.ICmsWebControl> pageControls =
currentPage.Staged.Controls; // let's delete all controls in the SideBarContent content place
holder foreach (Telerik.Cms.ICmsWebControl webCtrl in
pageControls)
{
if (webCtrl.ContainerID == "SideBarContent")
{
// in order to delete controls from the page we are going to need to get a reference to page
// associated with a transaction
Telerik.Cms.ICmsPage pageForEdit = cmsManager.GetPage(currentPage.ID, true) as Telerik.Cms.ICmsPage;
pageForEdit.Staged.RemoveControl(webCtrl.ID);
cmsManager.SavePage(pageForEdit);
}
} // finally we need to publish the current page and refresh it to see the results currentPage.Publish();
Response.Redirect(currentNode.Url);
|
See Also