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

Forums / Developing with Sitefinity / Get Generic Content on Page from Master Page

Get Generic Content on Page from Master Page

3 posts, 0 answered
  1. Jodie
    Jodie avatar
    59 posts
    Registered:
    09 Oct 2012
    31 Jan 2010
    Link to this post
    Hi guys, me again :)

    I am having some trouble getting all the generic contents .content on a page programmatically in SF 3.7 sp2. I have sort of asked this question before in another post here but thought I would start a new one.

    I have followed the suggestions/code in theses articles:
    1. Find Generic Content on the page
    2. Generic content url
    3. Get all instances of a Generic Content control (same code as 2)

    And this is the code I have at this point in my masterpage in the page_load event:
    Dim Manager As New Telerik.Cms.CmsManager() 
    Dim CurrentPage As Telerik.Cms.ICmsPage = Manager.GetPage(CmsUrlContext.Current.PageID)  
    Dim PageContent As String = String.Empty  
    For Each Ctrl In CurrentPage.Controls  
                If Not Ctrl.ControlType Is Nothing Then  
                    If Ctrl.ControlType.Name = "GenericContent" And Ctrl.ContainerID = "Content_Holder" Then  
                        Dim webControlGC As Telerik.Cms.Engine.WebControls.GenericContent = TryCast(Ctrl, Telerik.Cms.Engine.WebControls.GenericContent)  
                        If Not webControlGC Is Nothing Then  
                            PageContent += webControlGC.Content.ToString  
                        End If  
                    End If  
                End If  
    Next 

    I have tried many variations of the code examples btw and the suggestion in my original post, but the problem with all of the code in the examples and in my own is that when trying to cast the control as Generic Content it always fails as the control type is Telerik.Cms.ICmsWebControl, which makes sense, but this is in the examples from Telerik. In article No 1 above Luc uses 'For Each cmsProperty In cmsControl.Properties', however .properties is not a property of Telerik.Cms.ICmsWebControl.

    So how do I get the generic content .content?

    I should probably also mention the GC's are NOT shared content.

    This has had me in circles for a day or so now, I am probably doing or missing something stupid (happens a bit!), which is why I spent some time before I posted again.

    As always any help is appreciated.

    Thanks

    Jodie
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    31 Jan 2010
    Link to this post
    Hi Jodie,

    You could try using one of the following options

    1. Option one

    foreach (ICmsWebControl control in page.Controls)
    {
        if (control.ControlType == (typeof(GenericContent)))
        {
            GenericContent cnt = (GenericContent)control.LoadControl();
            if (cnt != null)
            {
                string contolContent = cnt.Content;
            }
        }
    }

    2. Option 2

    var holder = this.Page.Master.FindControl("Content") as ContentPlaceHolder;
          if (holder != null)
          {
              var gcControl = holder.FindControl("GenericContent1") as GenericContent;
              if (gcControl != null)
              {
                  string controlContent = gcControl.Content;
              }
          }


    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Jodie
    Jodie avatar
    59 posts
    Registered:
    09 Oct 2012
    11 Mar 2010
    Link to this post
    Hi Ivan,

    Just thought I'd let you know the above code didn't work exactly, there were no errors it still just wasn't returning the content. However I took my original code and modified this:

    Dim webControlGC As Telerik.Cms.Engine.WebControls.GenericContent = TryCast(Ctrl, Telerik.Cms.Engine.WebControls.GenericContent)
    To this:
    Dim webControlGC As Telerik.Cms.Engine.WebControls.GenericContent = TryCast(Ctrl.LoadControl(), Telerik.Cms.Engine.WebControls.GenericContent)
    And it worked like a charm :)

    Thanks for the help

    Jodie
Register for webinar
3 posts, 0 answered