代码之家  ›  专栏  ›  技术社区  ›  The real napster

ASP.NET UserControls-如何使内容字段可从aspx代码访问?

  •  0
  • The real napster  · 技术社区  · 17 年前

    假设我创建了一个名为MyUserControl.ascx的用户控件

    我用前缀uc在ascx中注册它

    <uc:MyUserControl runat="server" id="uc_test" SomeProperty="true">
    <InnerContent>
      ...
      Controls added in here....
      <asp:Button runat="server" id="btn_test" Text="Test">
      ...
    <InnerContent>
    </uc:MyUserControl>
    

    我知道如何创建usercontrol,以及如何向其添加属性和事件。

    但是如何在usercontrol中创建“InnerContent”字段?

    我不知道,所以请具体一点:)

    干杯

    3 回复  |  直到 17 年前
        1
  •  2
  •   Mehrdad Afshari    17 年前

    .ascx ).但这完全有可能。这主要是在使用代码文件构建的自定义控件中完成的。要完成此任务,请声明控件类,如下所示:

    [ParseChildren(true), PersistChildren(false)]
    public class MyControl : Control, INamingContainer {
    
       [PersistenceMode(PersistenceMode.InnerProperty),
        TemplateContainer(InnerContentTemplate)]
       public ITemplate InnerContent { get; set; }
    
       void CreateChildControls() { 
           InnerContentTemplate temp = new InnerContentTemplate();
           InnerContent.InstantiateIn(temp);
           Controls.Add(temp);
       }
    }
    
    public class InnerContentTemplate : Control, INamingContainer {
    
    }
    

    对于 您可以从中继承它的文件 UserControl 相反

        2
  •  0
  •   darasd    17 年前

    您希望创建一个模板化控件,而不是一个中继器(即,您有一个ItemTemplate区域,加上Header和wotnot)。 Here

        3
  •  0
  •   The real napster    17 年前

    在对Merhdad的答复所作评论的基础上: 这对我来说是可行的,但我似乎找不到一种方法来重新获得我在模板中添加的控件。关于这件事有什么快速的提示吗?真正的纳普斯特(14分钟前) 尝试 Control.FindControl 方法梅尔达德(8分钟前)

    ..................

    pnl_content.Controls[index]
    

    <uc:PopupOKCancel ClientInstanceName="pop_createCompany" runat="server" ID="pop_createCompany" OKButtonText="opret" HeaderText="Opret nyt firma">
         <ContentTemplate>
               <uc:CompanyDetails runat="server" id="uc_companyDetails"></uc:CompanyDetails>
         </ContentTemplate>
    </uc:PopupOKCancel>
    

    CompanyDetails用户控件实际上似乎不存在。。似乎只有模板存在。。我曾尝试向Template类添加一些属性,以便设置并获取它们,但这是不可能的。。出于某种原因。

    推荐文章