访问模板化用户控件ASP.NET中的子控件

WoF*_*gel 6 c# asp.net user-controls templates

以下代码已简化.

用户控件ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BaseFormControl.ascx.cs"
    Inherits="SOPR.CustomForms.BaseFormControl" %>
<fieldset class="fset1">
</fieldset>
Run Code Online (Sandbox Code Playgroud)

这是我的用户控件代码隐藏:

public partial class BaseFormControl : System.Web.UI.UserControl
    {



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



   void Page_Init()
        {




            if (Content != null)
            {
                ContentContainer cc = new ContentContainer();
                Content.InstantiateIn(cc);
                contentHolder.Controls.Add(cc);
            }
        }
Run Code Online (Sandbox Code Playgroud)

我在视图中的用法:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddOperator.aspx.cs"
    Inherits="SOPR.Cadastro.AddOperator" MasterPageFile="~/MasterPage.Master" %>

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" ID="maincont" runat="server"     EnableViewState="true">
    <uc:BaseFormControl ID="BaseFormControl1" runat="server">
        <Content>
      <asp:TextBox runat="server" CssClass="keytbcss" MaxLength="4" ID="keytb"
                NewLine="false" />
        </Content>
    </uc:BaseFormControl>
Run Code Online (Sandbox Code Playgroud)

我试图访问代码隐藏的"keytb"控件,但它就像它不存在(比如使用不存在的变量).有任何想法吗?

提前致谢.

解决方案--------------------------
我发现了一个非常好的解决方案,只需将[TemplateInstance(TemplateInstance.Single)]添加到ITemplate属性中即可.用户控制,一切都被看到.我现在可以使用就像它是一个普通的页面控件.

public partial class BaseFormControl : System.Web.UI.UserControl
{



[TemplateContainer(typeof(ContentContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateInstance(TemplateInstance.Single)]
public ITemplate Content { get; set; }
...
Run Code Online (Sandbox Code Playgroud)

WoF*_*gel 5

解决方案--------------------------
我发现了一个非常好的解决方案,只需将[TemplateInstance(TemplateInstance.Single)]添加到ITemplate属性中即可.用户控制,一切都被看到.我现在可以使用就像它是一个普通的页面控件.

public partial class BaseFormControl : System.Web.UI.UserControl
{



[TemplateContainer(typeof(ContentContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateInstance(TemplateInstance.Single)]
public ITemplate Content { get; set; }
...
Run Code Online (Sandbox Code Playgroud)