2009-10-30 78 views
1

我在ASP.NET項目中出現了一堆這種樣板代碼。ASP.NET:用戶控件可以訪問它包裝的控件

<div class="inputfield"> 
    <div class="tl"> 
    <span class="tr"><!-- --></span> 
    <span class="ll"><!-- --></span> 
    <div class="lr"> 
     <div class="cntnt"> 
     <asp:TextBox .../> 
     </div> 
    </div> 
    </div> 
</div> 

正如你可能已經猜到了,一切都在那個片段是除了最裏面的文本字段純樣板。

在ASP.NET中避免這種樣板文件的最佳方法是什麼?在例如Django的我會做一個自定義的標籤吧,因爲這樣的:

{% boiler %} 
<input ... /> 
{% endboiler %} 

我在想,也許我可以創建一個用戶控件,但我已經找到了在ASP.NET用戶控件的所有教程都非常簡單和「自動關閉」,即他們不知道標籤的內容。我需要的東西沿線︰

<Hello:MyControl> 
    <asp:TextBox .../> 
</Hello> 

所以我的問題是以下:什麼是避免樣板的最好方法?

回答

2

您可以使用ITemplate屬性。因此,您可以在不同情況下注入不同的內容。

[PersistChildren(false), ParseChildren(true, "ContentTemplate")] 
public partial class WebUserControl1 : System.Web.UI.UserControl 
{ 

    [System.ComponentModel.Browsable(false), System.Web.UI.PersistenceMode(PersistenceMode.InnerProperty)] 
    public ITemplate ContentTemplate { get; set; } 

    protected override void CreateChildControls() 
    { 
     if (this.ContentTemplate != null) 
      this.ContentTemplate.InstantiateIn(this); 

     base.CreateChildControls(); 
    } 
} 
+0

對不起,但我真的不明白如何使用此代碼段。在這種情況下,.ascx文件的外觀如何? – 2009-11-02 13:08:13

+0

我想我現在明白了,但仍然有一個問題......如果我不想使用ContentTemplate或類似的東西,該怎麼辦? – 2009-11-02 14:45:38

+0

你爲什麼不想要那個? – 2009-11-02 17:11:25

0

將asp:TextBox與其他html標籤一起放在用戶控件中。提供匹配的文本框的屬性,關於你的用戶控件的屬性,所以,你會做這樣的事情:

<Hello:MyControl ID="myControl" runat="server" Width="300px" MaxLength="30" /> 

,然後寬度和最大長度的屬性會一下就傳送到內部文本框。

您還可以從usercontrol提供對文本框的訪問權限,並在代碼後面設置所有屬性。

+0

這還不夠,因爲應該可以將任何內容放入控件中,而不僅僅是文本框。 – 2009-10-30 15:44:57

+0

啊 - 對不起,我錯過了這部分的問題。 – 2009-10-30 16:16:10

+0

我現在意識到,我應該更清楚的那部分! :) – 2009-10-30 16:36:55

0

創建一個類是這樣的:

[PersistChildren(false), ParseChildren(true, "ContentTemplate")] 
public class CustomContent:WebControl 
{ 
    [System.ComponentModel.Browsable(false), System.Web.UI.PersistenceMode(PersistenceMode.InnerDefaultProperty)] 
    public ITemplate ContentTemplate { get; set; } 

    private PlaceHolder m_placeHolder; 
    protected override void CreateChildControls() 
    { 
     m_placeHolder = new PlaceHolder(); 

     if (this.ContentTemplate != null) 
      this.ContentTemplate.InstantiateIn(m_placeHolder); 

     Controls.Add(m_placeHolder); 
    } 

    protected override void RenderContents(HtmlTextWriter writer) 
    { 
     writer.Write(@"<div class=""inputfield""> 
<div class=""tl""> 
<span class=""tr""><!-- --></span> 
<span class=""ll""><!-- --></span> 
<div class=""lr""> 
    <div class=""cntnt""> 
"); 
     base.RenderContents(writer); 

     writer.Write(@"  </div> 
</div> 
</div> 
</div> 
"); 
    } 

} 

該類不是一個「用戶控制」這是一個「服務器控制」。你可以用用戶控件做同樣的事情,但是你會遇到設計師的問題。這將在設計師工作。 你可以把類似這樣的標記在你的ASPX:

<uc1:CustomContent runat="server" ID="content"> 
     <asp:textbox runat="server"></asp:textbox> 
    </uc1:CustomContent> 

不要忘了在ASPX上方的註冊頁聲明

<%@ Register tagprefix="uc1" Assembly="Assembly where CustomContent is" Namespace="namespace where CustomContent is" %> 

你可以把任何你想要的UC1內: CustomContent標籤,它會在其周圍呈現該樣板html。如果您對ITemplate的工作方式感到好奇,那麼msdn上有很多文章,等等。