+1-888-365-2779
Try Now
More in this section

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

adding a few controls to it programatically

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

    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()
        {
            base.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);                   
                        staged.CheckIn();
                        page.Publish();      
                         
     
                    }
                }
            }
        }
     
    }

    3.App_Master/mytemplate.master
    <%@ 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">
        <title></title>
    <script type="text/C#" runat="server">

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

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    05 Jan 2011
    Link to this post
    Hello humayoo,

    This error is thrown, because you need to get the page for editing one more time before publish it.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. humayoo
    humayoo avatar
    89 posts
    Registered:
    25 Oct 2010
    06 Jan 2011
    Link to this post
    hi,

    i am not getting it .how to get the page for editing one more time before publish it. please provide some example or clue , how to do it ?

    thanks
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    06 Jan 2011
    Link to this post
    Hello humayoo,

    Use GetPage method of the CmsManager.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. humayoo
    humayoo avatar
    89 posts
    Registered:
    25 Oct 2010
    06 Jan 2011
    Link to this post
    Hi,

    Now i am able to add user control but i have two problem.

    1. when i select template, user control(made my me) is added but it was not showing. it was showing after publish.please see attachment picture notshowing.png

    2.when user control added to the page when i click edit page it was showing that index was outside the bounds of array.  here is my code.

    page is \Sitefinity\Admin\EditPage.aspx

    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;
    using System.Collections;
    using Telerik.Cms.Data;
     
    public class EditPageCustom : EditPage
    {
     
        private string PublishPage = string.Empty;
     
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            CmsManager.Executed += new EventHandler<Telerik.ExecutedEventArgs>(CmsManager_Executed);
        }
     
        void CmsManager_Executed(object sender, Telerik.ExecutedEventArgs e)
        {
            PublishPage = e.CommandName;
        }
     
        // override CreateChildControls method.
        protected override void CreateChildControls()
        {
            base.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 && toolbar.StagedPage.Parent.Status.ToString()=="New" && !IsPostBack)
            {
                // 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("contacts"))
                    {             
                        
                        CmsManager manager = new CmsManager();                  
                        Telerik.Cms.Engine.WebControls.GenericContent gc1 = new Telerik.Cms.Engine.WebControls.GenericContent();
                        gc1.Content = "<strong>Hello world!</strong>";
                        UserControl MyEvent = new UserControl();
                        MyEvent = (UserControl)this.Page.LoadControl("~/UserControls/mycustomcontrol/myTest.ascx");
                        //PlaceHolder1.Controls.Add(userControl); 
                        //Control MyEvent = Page.LoadControl("~/UserControls/mycustomcontrol/myTest.ascx");
                        toolbar.StagedPage.Parent.Staged.AddControl("maincontentdetails",MyEvent );
                        manager.GetPage(toolbar.StagedPage.Parent.Staged.ID, true);
                        manager.GetPage(toolbar.StagedPage.Parent.Staged.ID, true);
                        //manager.SavePage(manager.GetPage(toolbar.StagedPage.Parent.Staged.ID,true));
                         
                            
                         
     
                    }
                }
            }
        }
     
     
     
    }

    please guide me how to solve this problem in details and step by step procedure
  6. humayoo
    humayoo avatar
    89 posts
    Registered:
    25 Oct 2010
    07 Jan 2011
    Link to this post
    hi,

    i am waiting for reply of above post. please reply as soon as possible.
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    10 Jan 2011
    Link to this post
    Hello humayoo,

    You have to use one of the methods

    • AddControl(string containerId, string url, int ordinal, string controlId)
    • AddControl(string containerId, Type type, string controlId)


    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
7 posts, 0 answered