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

Forums / Developing with Sitefinity / Finding controls inside a GenericContent control

Finding controls inside a GenericContent control

5 posts, 0 answered
  1. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    16 Apr 2010
    Link to this post
    I am building a control that needs to find and target client side tags like <a:href> or <img> that might be inside a GenericContent control that the control uses.

    Could someone help with with a possible approach of how to do this?

    Duncan
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    16 Apr 2010
    Link to this post
    Hi Duncan Evans,

    First you need to find the GenericContent control on a page. Then get the control Content property which is of type string. You can use regular expression to match the tags you are looking for.

    sample

    IList<Telerik.Cms.ICmsWebControl> pageControls = currentPage.Staged.Controls;
    foreach (Telerik.Cms.ICmsWebControl webCtrl in pageControls)
    {
     if (webCtrl.ContainerID == "SideBarContent" && webCtrl.ControlType == typeof(Telerik.Cms.Engine.WebControls.GenericContent))
     {
         Telerik.Cms.Engine.WebControls.GenericContent gcCtrl = webCtrl.LoadControl() as Telerik.Cms.Engine.WebControls.GenericContent;
         string controlContent = gcCtrl.Content;
     
         // here look for the tags
     }
    }


    Greetings,
    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. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    16 Apr 2010
    Link to this post
    I actually just realized that as you posted this.

    Do i have to search for the control in the way you suggested or can i just do something like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        string content = GenericContent1.Content;
     
        string expression = @"<A[^>]*?HREF\s*=\s*[""']?([^'"" >]+?)[ '""]?>";
     
        MatchEvaluator evaluator = new MatchEvaluator(UpdateLink);
     
        string ModifiedContent = Regex.Replace(content, expression, evaluator, RegexOptions.IgnoreCase);
     
        GenericContent1.Content = ModifiedContent;
    }
  4. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    17 Apr 2010
    Link to this post
    Ivan,

    I ran into a snag with this. If the content of the GenericContent control is shared content, then all i get back in the string is 9fec9480-0e46-4d75-97e4-804bd41030b6

    So how do i find the actual content?

    Duncan
  5. Duncan Evans
    Duncan Evans avatar
    122 posts
    Registered:
    07 Jul 2009
    17 Apr 2010
    Link to this post
    Oh, nevermind... i figured it out:

    string content = string.Empty;
    if (GenericContent1.SharedContent != null)
    {
        IContent sharedContent = GenericContent1.SharedContent;
        content = sharedContent.Content.ToString();
    }
    else
    {
        content = GenericContent1.Content;
    }
Register for webinar
5 posts, 0 answered