2013-07-24 39 views
0

我想創建一個自定義控件,我應該能夠有一些像ASP面板中的子控件。自定義控件與內部HTML C#

<custom:panel runat="server" id="CustomPanel"> 
    <asp:TextBox id="textbox1" runat="server" /> 
    <span>Test</span> 
</custom:panel> 

我的目標是創建一個自定義容器,並具有與上面相同的innerhtml。我應該能夠在頁面後面的代碼中訪問如下所示的服務器端控件。

textbox1.Text="something"; 

感謝

回答

0

請看以下解決方案。我們可以使用它來創建像圓角容器等控件。

[ParseChildren(true, "Items")] 
[PersistChildren(true)] 
public class CustomContainer : Control 
{ 
    private List<Control> m_items = new List<Control>(); 


    [Browsable(false)] 
    public List<Control> Items 
    { 
     get { return m_items; } 
    } 


    protected override void CreateChildControls() 
    { 
     //create a custom container of our choice for your child controls 
     HtmlGenericControl div = new HtmlGenericControl("div"); 
     foreach (Control ctrl in Items) 
     { 
      div.Controls.Add(ctrl); 
     } 
     this.Controls.Add(div); 
    }   
}