繁体   English   中英

从用户控件加载模板

[英]Load template from user control

我正在与Sitefinity合作,并且正在开发控件设计器-但是,我不认为我的问题是针对SiteFinity的。

我有一个像这样的课程:

公共类CaseStudyFeaturedItem:CaseStudySelectorControlDEsignerBase

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

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

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

所以从本质上讲,我有usercontrol.ascx,它是嵌入式资源,因此具有类似以下的字符串:

mynamespace.myclass.usercontrol.ascx;

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

这可能吗?

谢谢Al

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

我们对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控件中完成所有操作的,您可能应视情况进行些微修改。

干杯,
斯拉沃
Sitefinity团队@ Telerik

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM