2013-07-24 40 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); 
    }   
}