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

Forums / Developing with Sitefinity / How to dynamically add one or more generic content

How to dynamically add one or more generic content

3 posts, 0 answered
  1. vivek garg
    vivek garg avatar
    13 posts
    Registered:
    20 Aug 2009
    24 Aug 2009
    Link to this post
    Hi All,

    How can I add either one / two / three "generic content control" in my page.
    Generic Content provided by Sitefinity.
    This will be as part of configuration, so that non developer can easily do that.
    Can you suggest the appropriate one to achieve it?

    Thanks,
    Vivek
  2. vivek garg
    vivek garg avatar
    13 posts
    Registered:
    20 Aug 2009
    24 Aug 2009
    Link to this post
    All of the Generic Content control would be used in the same row.
    So I can't add continuous generic control.
  3. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    25 Aug 2009
    Link to this post
    Hello vivek garg,

    Here is an example on creating page and adding a few controls to it programatically:
        protected void create_Click(object sender, EventArgs e) 
        { 
            if (this.TextBox1.Text.Length > 0) 
            { 
                CmsManager manager = new CmsManager(); 
     
                // Get some page template ID 
                Guid templateId = ((IPageTemplate)manager.GetTemplates()[0]).ID; 
     
                ICmsPage page = manager.CreatePage(this.TextBox1.Text); 
                page.Navigable = true
                page.MenuName = this.TextBox1.Text; 
                //page.ParentID = locationsPage.ID; 
                page.Title = this.TextBox1.Text; 
     
                //We can skip checkout on creating the page 
                //but it is recommended in order to comply with the lifecycle 
                IStagedPage staged = page.Staged.CheckOut(); 
                staged.SetTemplate(templateId, null); 
     
                LiteralControl lit = new LiteralControl(); 
                lit.Text = "<h1>This is test control</h1><br>"
                staged.AddControl("Content", lit); 
     
                //**** We need to get the page one more time in order to be JOINED to a transacton :) 
                //Note, the page is retrieved from the thread cache 
                page = (ICmsPage)manager.GetPage(page.ID, true); 
                LiteralControl lit2 = new LiteralControl(); 
                lit2.Text = "<h2>This is test control 2</h2><br>"
                staged.AddControl("Content", lit2); 
     
                page = (ICmsPage)manager.GetPage(page.ID, true); 
                staged.CheckIn(); 
     
                page.Publish(); 
            } 
        } 

    You can skip the parts that you do not want, and change the literal control with GenericContent. 

    Note that you should provide a separator between the controls, for example <br />. Otherwise, the rendered content of each control will be tied with the previous one. 

    Regards,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
Register for webinar
3 posts, 0 answered