繁体   English   中英

在asp.net中创建自定义模板控件

[英]creating custom templated control in asp.net

我想创建自定义模板控件,该控件可以在页面中获取模板内部的控件(类似于updatepanel的行为)。 因此,请发布更多详细信息。 该控件必须看起来像:

<ec:TabControl runat="server" ID="tab">
    <Tabs>
        <ec:Tab runat="server">
            <TabContainer>
                <asp:Button runat="server" Text="aaaaaa" />
            </TabContainer>
            <TabName>
                text or controls
            </TabName>
        </ec:Tab>
        <ec:Tab runat="server">
            <TabContainer>
                <asp:Button runat="server" Text="vcxvxvxv" />
            </TabContainer>
            <TabName>
                some text
            </TabName>
        </ec:Tab>
    </Tabs>
</ec:TabControl>

当数据绑定控制运行时,它可以很好地工作。 换句话说,在ondatabound阶段,将适当实例化内部的所有控件和模板。 但是我要做的是可以直接从页面(按ID)访问内部的控件。 例如,您可以使用updatepanel(在页面范围内提供的内容)来完成此操作。

在下面您可以看到控件的源代码片段:

public class Tab
{
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [TemplateContainer(typeof(HtmlAnchorContainer))]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public ITemplate TabName { get; set; }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    [TemplateContainer(typeof(PanelContainer))]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public ITemplate TabContainer { get; set; }
}

public class TabControl : System.Web.UI.WebControls.WebControl
{
    List<Tab> tabs;
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
    PersistenceMode(PersistenceMode.InnerProperty),
    NotifyParentProperty(true)]
    public List<Tab> Tabs
    {
        get { return tabs ?? new List<Tab>(); }
        set { tabs = value; }
    }

    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        .....
        foreach (Tab tabItem in Tabs)
        {
            //generating tree control for further rendering
        }
        ...
    }

    protected override void OnDataBinding(EventArgs e)
    {
        EnsureChildControls();
        base.OnDataBinding(e);
    }
 }

希望为您提供建议,建议,链接和建设性的批评。))

申请:

[TemplateInstance(TemplateInstance.Single)]

声明到要直接引用控件的模板,则应解决此问题。

暂无
暂无

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

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