简体   繁体   English

如何使用INamingContainer创建自定义控件?

[英]How to create a custom control with INamingContainer?

I am trying the following: 我正在尝试以下方法:

[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateContainer(typeof(TemplateContainer))]
public virtual ITemplate LayoutTemplate { get; set; }

protected void Page_Init(object sender, EventArgs e)
{
    this.Controls.Clear();

    if (LayoutTemplate != null)
    {
        var data = Enumerable.Range(0, 10);

        foreach (int index in data)
        {
            TemplateContainer container = new TemplateContainer(index);

            LayoutTemplate.InstantiateIn(container);

            this.Controls.Add(container);
        }
    }
}

My container class: 我的容器类:

public class TemplateContainer : Control, INamingContainer
{
    public int Index { get; set; }

    internal TemplateContainer(int index)
    {
        this.Index = index;
    }
}

And my markup: 和我的标记:

<uc:TemplateControl ID="ucTemplateControl" runat="server">
    <LayoutTemplate>
        <b>Index:</b>
        <asp:TextBox ID="Literal1" runat="server"
            Text='<%# Container.Index %>'
            ReadOnly="true" />
        <br />
    </LayoutTemplate>
</uc:TemplateControl>

But for some reason Container.Index is not rendering any value, just empty. 但是由于某种原因, Container.Index没有呈现任何值,只是空的。 10 controls are being created, but none shows a value. 正在创建10个控件,但没有一个显示值。

What did I do wrong? 我做错了什么? How can I fix it so it will show the Index value? 我该如何解决它才能显示Index值?

I tried something similar to MSDN example: 我尝试了类似于MSDN示例的内容:

To bind the value you need to call the DataBind method of the custom control. 要绑定值,您需要调用自定义控件的DataBind方法。

Calling 呼唤

ucTemplateControl.DataBind();

from the page made the data to be bound on the template. 从页面上将数据绑定到模板上。

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

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