Try Now
More in this section

Forums / Developing with Sitefinity / adding a few controls to it programatically

adding a few controls to it programatically

1 posts, 0 answered
  1. humayoo
    humayoo avatar
    89 posts
    25 Oct 2010
    05 Jan 2011
    Link to this post

    i am creating page manually but when user select the template from template drop down list then i am adding few controls pro grammatically to it. but i am getting following error you can not change a value of object that is not in transaction. i am doing like this.

    1. \Sitefinity\Admin\EditPage.aspx and code is
    <%@ Page Inherits="EditPageCustom, App_Code" MasterPageFile="~/Sitefinity/Dummy.master" %>

    2. App_code/EditPageCustom.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Cms.Web;
    using Telerik.Cms.Web.UI;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Cms;
    using Telerik.Cms.Engine.WebControls;
    public class EditPageCustom : EditPage
        // override CreateChildControls method.
        protected override void CreateChildControls()
            //Find the toolbar control
            ToolBar toolbar = this.Form.FindControl("toolbar") as ToolBar;
            // find the container where themes DropDownList control is located
            // here you can use recursion as well.
            Control cntr = toolbar.Controls[0].Controls[0];
            cntr = cntr.Controls[cntr.Controls.Count - 1];
            cntr = cntr.Controls[cntr.Controls.Count - 1];
            cntr = cntr.Controls[cntr.Controls.Count - 1];
            // check whether there is container and toolbar control.
            if (cntr != null && toolbar != null)
                // find the DropDownList control
                DropDownList ddList = cntr.FindControl("templates") as DropDownList;
                // loop through the list items
                foreach (ListItem item in ddList.Items)
                    // do something here or manage ddList control
                    if (item.Selected && item.Text.Equals("mytemplate"))
                        CmsManager manager = new CmsManager();
                        ICmsPage page = (ICmsPage)manager.GetPage(toolbar.StagedPage.Parent.Staged.ID, true);
                        //We can skip checkout on creating the page
                        ////but it is recommended in order to comply with the lifecycle
                        IStagedPage staged = page.Staged.CheckOut();
                        Telerik.Cms.Engine.WebControls.GenericContent gc1 = new Telerik.Cms.Engine.WebControls.GenericContent();
                        gc1.Content = "<strong>Hello world!</strong>";                  
                        staged.AddControl("maincontentdetails", gc1);                   

    <%@ Master Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head id="Head1" runat="server">
    <script type="text/C#" runat="server">

        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <!--WRAPPER START-->
        <div class="wrapper">
        <!--HEADER START-->
            <div class="header">
            <span class="logo">      
           <asp:ContentPlaceHolder ID="headercompanylogo" runat="server"></asp:ContentPlaceHolder>
        <!--NAV START-->
        <ul class="nav">
            <asp:ContentPlaceHolder ID="topmenu" runat="server"></asp:ContentPlaceHolder>
        <!--CONTENT START-->
            <div class="contentInner">
                 <asp:ContentPlaceHolder ID="maincontentdetails" runat="server"></asp:ContentPlaceHolder>
       <!--FOOTER START-->
            <div class="footer">

1 posts, 0 answered