views:

42

answers:

2

Hello,

Im working with Sitefinity and I'm developing a Control Designer - however i dont think my question is specific to SiteFinity.

I have a class such as:

public class CaseStudyFeaturedItem : CaseStudySelectorControlDEsignerBase

The class it is inherriting from is itself inheriting from UserControl, like so:

public class CaseStudySelectorControlDesignerBase : System.Web.UI.UserControl {

Within CaseStudyFeaturedItem is it possible to load a template which is an embedded resource and then access the controls on that control?

So essentially, I have usercontrol.ascx which is an embedded resource so has a string like:

mynamespace.myclass.usercontrol.ascx;

And from within CaseStudyFeaturedItem I want to be able to load that usercontrol and then modify the controls (i.e. literals/labels) that are within it?

Is this possible?

Thanks Al

A: 

Yes it is possible, but I'm not entirely sure what you're trying to accomplish based on your question. You can use LoadControl to dynamically load user controls. If you cast the result to the appropriate control type, you will then have access to all of its properties. From there, you can add it into whatever container you want to hold it. Is that the kind of thing you're trying to do?

Ocelot20
A: 

Hi higgsy,

We do this with every control in Sitefinity, but it would be a little complicated to do with your own custom controls (I assume you are using Sitefinity 3.7). The steps are the following:
- Implement a template container control, inheriting from GenericContainer:

protected class ItemListContainer : GenericContainer
{
    public virtual Repeater RepeaterControl
    {
        get { return base.GetControl<Repeater>("repeater", true); }
    }
}

- You need to get the template from the resource (use ControlUtils.GetTemplate method - Sitefinity does that for you):

public virtual ITemplate ItemListTemplate
{
    get
    {
        if (itemListTemplate == null)
            itemListTemplate = ControlUtils.GetTemplate(<virtual path to template>, <resource file name>,
                <type to determine assembly for template>);
        return itemListTemplate;
    }
    set
    {
        itemListTemplate = value;
    }
}

- You need to call InstantiateIn method of the template, and pass it the container control

listContainer = new ItemListContainer();
ItemListTemplate.InstantiateIn(listContainer);

- Access all controls through the container

listContainer.RepeaterControl.DataBind();

In Sitefinity 4.0 we've included a base class for all controls, which will give you this functionality out of the box. In 3.7 though, you'll have to do all this by hand.

The ControlUtils class is in the Telerik.Framework.Web namespace. The code above is how this all is done in the ContentView control, you should probably make slight modifications depending on your case.

Cheers,
Slavo
The Sitefinity team @ Telerik

Slavo
Thanks Slavo - perfect
higgsy