从用户控件加载模板

时间:2010-09-30 08:50:58

标签: c# asp.net sitefinity

我正在使用Sitefinity而我正在开发一个控制设计器 - 但是我不认为我的问题是针对SiteFinity的。

我有一个类如:

公共类CaseStudyFeaturedItem:CaseStudySelectorControlDEsignerBase

它继承自的类本身继承自UserControl,如下所示:

公共类CaseStudySelectorControlDesignerBase:System.Web.UI.UserControl {

在CaseStudyFeaturedItem中,是否可以加载作为嵌入式资源的模板,然后访问该控件上的控件?

基本上,我有一个usercontrol.ascx这是一个嵌入式资源,所以有一个字符串,如:

mynamespace.myclass.usercontrol.ascx;

从CaseStudyFeaturedItem中我希望能够加载该usercontrol,然后修改其中的控件(即文字/标签)?

这可能吗?

由于 人

2 个答案:

答案 0 :(得分:0)

是的,这是可能的,但我不完全确定你想要根据你的问题完成什么。您可以使用LoadControl动态加载用户控件。如果将结果转换为适当的控件类型,则可以访问其所有属性。从那里,您可以将其添加到您想要容纳它的任何容器中。这是你想要做的事情吗?

答案 1 :(得分:0)

我们使用Sitefinity中的每个控件执行此操作,但使用自己的自定义控件(我假设您使用的是Sitefinity 3.7)会有点复杂。步骤如下:
- 实现模板容器控件,继承自GenericContainer

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

- 您需要从资源中获取模板(使用ControlUtils.GetTemplate方法 - Sitefinity为您执行此操作):

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;
    }
}

- 您需要调用模板的InstantiateIn方法,并将其传递给容器控件

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

- 通过容器访问所有控件

listContainer.RepeaterControl.DataBind();

在Sitefinity 4.0中,我们为所有控件都包含了一个基类,它将为您提供开箱即用的功能。但是在3.7中,您必须手动完成所有这些操作。

ControlUtils类位于Telerik.Framework.Web命名空间中。上面的代码是如何在ContentView控件中完成所有操作,您应该根据您的情况稍作修改。

干杯,
Slavo
Sitefinity团队@ Telerik