2009-08-17 75 views
0

我試圖建立一個模板控件。你會看到,作爲字段的一部分,我希望能夠將控件定義爲兒童。當我嘗試編譯時,我收到錯誤消息「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 
{ 
    . 
    . 
    . 
} 

回答

0

GridView控件也不允許這樣操作, 而是使用MyForm1.FindControl( 「myButton的」)

我也有看到你的模板不從了Itemplate派生接口。像往常一樣,ASP.Net也使用System.Web.UI.Control作爲這個impl的基類。

2

在你的實現中有一些奇怪的東西。很難理解你想要做什麼,要麼建立一個模板控件,要麼你可以添加一個子控件列表的控件。

對於模板方法,你不得不做這樣的事情:

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

0

我用下面的代碼:

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> 

這將是框架

的底部