Contents
Core Concepts
Sitefinity in Visual Studio
Sitefinity Building Parts
Developing with Sitefinity
Designing with Sitefinity
Security
How-to
API Reference
|
|
| Creating Controls |
Send comments on this topic. |
| See Also |
|
Developing with Sitefinity > Pages > Pages API Walkthrough > Controls > Creating Controls |
This topic demonstrates how to create new controls and add them to a Sitefinity page. Remember that when working with Controls on Sitefinity pages you should be using the
Staged version of a page, represented by IStagedPage interface.
Create a new instance of Sitefinity built-in control and add it to the page:
 |
This example is using Generic Content control, because it is very simple to set up, but you can add any
Sitefinity built-in control (like BlogPosts, NewsView and so on) in same manner. |
|
Copy Code |
|
// create a new instance of CmsManager Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager(); // create a new Generic Content control (note that you can
add any kind of controls, including
// Sitefinity controls, user controls, custom controls and standard asp.net control) Telerik.Cms.Engine.WebControls.GenericContent gc1 = new Telerik.Cms.Engine.WebControls.GenericContent();
gc1.Content = "<strong>Hello world!</strong>"; // now
that we have a control, we need to get a reference to a page where we want to
// add this control Telerik.Cms.Web.CmsSiteMapNode currentPage =
(Telerik.Cms.Web.CmsSiteMapNode)SiteMap.Provider.CurrentNode; // to be able to add a control to a page we need a instance
of page associated with a transaction Telerik.Cms.ICmsPage cmsPage = cmsManager.GetPage(currentPage.CmsPage.ID,
true) as Telerik.Cms.ICmsPage; // now we are ready to add the control
to the page
// notice that we are adding control to the Staged version of a page, accessing it through the
// Staged property of IPage interface (remember that ICmsPage interface implement IPage interface).
// The first argument is the id of the content place holder to which the control should be added
// (we are assuming that the underlying master page has a "SideBarContent" content place holder),
// while the second argument is the control to be added cmsPage.Staged.AddControl("SideBarContent", gc1); // finally we need to publish the
page cmsPage.Publish(); // since we have added the control to the current page, we'll need
to refresh the page
// to see the effect Response.Redirect(currentPage.Url);
|
Create a new instance of a standard ASP.NET control and add it to a page:
|
Copy Code |
// create a new instance of CmsManager Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager(); // create a new standard ASP.NET HyperLink control (note
that you can add any kind of controls, including
// Sitefinity controls, user controls, custom controls and standard asp.net control) HyperLink link1 = new HyperLink();
link1.Text = "Sitefinity";
link1.NavigateUrl = "http://www.sitefinity.com"; // now that we
have a control, we need to get a reference to a page where we want to
// add this control Telerik.Cms.Web.CmsSiteMapNode currentPage =
(Telerik.Cms.Web.CmsSiteMapNode)SiteMap.Provider.CurrentNode; // to be able to add a control to a page we need a instance
of page associated with a transaction Telerik.Cms.ICmsPage cmsPage = cmsManager.GetPage(currentPage.CmsPage.ID,
true) as Telerik.Cms.ICmsPage; // now we are ready to add the control
to the page
// notice that we are adding control to the Staged version of a page, accessing it through the
// Staged property of IPage interface (remember that ICmsPage interface implement IPage interface).
// The first argument is the id of the content place holder to which the control should be added
// (we are assuming that the underlying master page has a "SideBarContent" content place holder),
// while the second argument is the control to be added cmsPage.Staged.AddControl("SideBarContent", link1); // finally we need to publish the
page cmsPage.Publish(); // since we have added the control to the current page, we'll need
to refresh the page
// to see the effect Response.Redirect(currentPage.Url);
|
 |
The ICmsPage.Staged.AddControl method allows you to add controls in various ways, so you can add control
by specifying its type, the actual instance or even the URL if you are adding a User Control. |
See Also
|