【问题标题】:How to create a custom control with INamingContainer?如何使用 INamingContainer 创建自定义控件?
【发布时间】:2012-04-05 12:33:20
【问题描述】:

我正在尝试以下方法:

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

我的容器类:

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

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

还有我的标记:

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

但由于某种原因,Container.Index 没有呈现任何值,只是空的。正在创建 10 个控件,但没有一个显示值。

我做错了什么?如何修复它以显示Index 值?

我尝试了类似于 MSDN 示例的方法:

【问题讨论】:

标签: c# asp.net controltemplate naming-containers


【解决方案1】:

绑定值需要调用自定义控件的DataBind方法。

打电话

ucTemplateControl.DataBind();

从页面制作的数据要绑定到模板上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多