ASP.NET模板控制

问题描述:

我试图建立一个模板控件。你会看到,作为字段的一部分,我希望能够将控件定义为儿童。当我尝试编译时,我收到错误消息“MyTemplateControl.Field”没有名为'Button'“的公共属性。有谁知道如何完成我想要做的事情?ASP.NET模板控制

下面您会看到一个XHTML标记示例,下面是我的控件实现。

我编辑了我的例子,希望澄清我在找什么。谢谢大家的MSDN链接。我已经通过了这些。
我想要建立的是一个数据输入控件,它会自动为我设置一个表格。我们做了大量的数据录入Web表单,并且希望能够缩短实施时间。

<cc1:MyForm ID="MyForm1" runat="server"> 
    <ViewTemplate> 
     <cc1:Field Question="What is your name?"> 
      <asp:Label ID="myLabel" runat="server" /> 
     </cc1:Field> 
    </ViewTemplate> 
    <EditTemplate> 
     <cc1:Field Question="What is your name?"> 
      <asp:Textbox ID="myTextbox" runat="server" /> 
     </cc1:Field> 
    </EditTemplate> 
</cc1:MyForm> 

public class MyForm : WebControl, INamingContainer 
{ 
    private FieldCollection _fieldCollection; 
    private FieldCollection _field2Collection; 

    public FieldCollection ViewTemplate 
    { 
     get 
     { 
      if (_fieldCollection == null) 
      { 
       _fieldCollection = new FieldCollection(); 
      } 
      return _fieldCollection; 
     } 
    } 

    public FieldCollection EditTemplate 
    { 
     get 
     { 
      if (_field2Collection == null) 
      { 
       _field2Collection = new FieldCollection(); 
      } 
      return _field2Collection; 
     } 
    } 
} 

public class FieldCollection : CollectionBase 
{ 
    . 
    . 
    . 
} 

[ParseChildren(false)] 
public class Field 
{ 
    . 
    . 
    . 
} 

GridView控件也不允许这样操作, 而是使用MyForm1.FindControl( “myButton的”)

我也有看到你的模板不从了Itemplate派生接口。像往常一样,ASP.Net也使用System.Web.UI.Control作为这个impl的基类。

在你的实现中有一些奇怪的东西。很难理解你想要做什么,要么建立一个模板控件,要么你可以添加一个子控件列表的控件。

对于模板方法,你不得不做这样的事情:

public class MyForm : WebControl, INamingContainer 
{ 

    private TemplateOwner templateOwner{ get; set; } 

    private ITemplate _Content; 
    public ITemplate Content 
    { 
     get 
     { 
     return this._Content; 
     } 
     set 
     { 
     _Content = value; 
     } 
    } 

    protected override void CreateChildControls() 
    { 
     base.Controls.Clear(); 

     templateOwner = new TemplateOwner(); 
     this.Content.InstantiateIn(templateOwner); 

     this.Controls.Add(templateOwner); 
    } 

    ... 
} 

[ToolboxItem(false)] 
public class TemplateOwner : WebControl{ 

    public TemplateOwner():base(HtmlTextWriterTag.Div) 
    { 
    } 
} 

的使用会再看看像

<cc1:MyForm ID="MyForm1" runat="server"> 
    <Content> 
     <!-- Place whatever HTML, ASP.net controls etc you like --> 
    </Content> 
</cc1:MyForm> 

你还必须添加相应的注释来配置行为的设计师。我只是很快写下了这个想法给你一个想法。

+0

关闭,但我需要它更进一步延长。看到我编辑的问题。 – user62064 2009-08-17 14:41:31

+0

......好吧。只需添加另一个属性,如类型为ITemplate的“内容”并继续。它应该工作。你的东西与收集是不需要的。在一个模板中,你可以放任何你想要的,所以你可以忘记FieldCollection。否则,你想实现的不是一个模板,你必须去FieldCollection方式。这是两件不同的事情。 – Juri 2009-08-17 15:52:12

+0

这是否适用于WebApplicationProject? – Mahes 2011-01-05 20:33:03

我用下面的代码:

public partial class SmallFramedControl : UserControl, INamingContainer 
{ 
    private TemplateOwner templateOwner { get; set; } 
    private ITemplate _Content; 
    public ITemplate Content 
    { 
     get 
     { 
      return this._Content; 
     } 
     set 
     { 
      _Content = value; 
     } 
    } 

    protected override void CreateChildControls() 
    { 
     //base.Controls.Clear(); 
     templateOwner = new TemplateOwner(); 
     this.Content.InstantiateIn(templateOwner); 
     plchAdd.Controls.Add(templateOwner); 
    } 



    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
} 

[ToolboxItem(false)] 
public class TemplateOwner : WebControl 
{ 
    public TemplateOwner() 
     : base(HtmlTextWriterTag.Div) 
    { 
    } 
} 

和ASCX页的样子:

<%@ Control Language="C#" AutoEventWireup="true" 
      CodeBehind="SmallFramedControl.ascx.cs" 
      Inherits="OtPortalNewDesign.UserControls.SmallFramedControl" %> 

这将是顶部框架

<asp:PlaceHolder ID="plchAdd" runat="server"></asp:PlaceHolder> 

这将是框架

的底部