2009-11-05 55 views
1

我正在尋找創建一個WebControl,它可以在其中放置標記,並自己動態創建子控件。我遇到的問題是,我無法(但)將標記中的控件(請參見下面的示例)與我創建的子控件分開。ASP.NET WebControl和渲染包含子代

我知道我需要設置類與這些2個標誌:

[ParseChildren(false)] 
[PersistChildren(true)] 
public class OuterControl : WebControl 
{ 
    ... 
} 

而且樣品的標記看起來像:

<custom:OuterControl> 
    <asp:TextBox ...> 
<custom:OuterControl> 

RenderContents(),我有我需要將一些控件添加到控件樹中,渲染,然後渲染包裝在特定部分的標記中的控件。例如: -

protected override void RenderContents(HtmlTextWriter output) 
{ 
    EnsureChildControls(); 
    [ Misc work, render my controls ] 

    [** Would like to render wrapped children here **] 

    [ Possibly other misc work ] 
} 

如前所述,我可以讓我的代碼創建的控件從調用RenderChildren渲染兩次(),或包裝的控制,以不通過刪除線呈現在所有。該死。

想法?

回答

1

當我有類似的要求(建設一組標準的各地供應的控制控件),我最後做這樣的事情:

EnsureChildControls(); 

Control[] currentControls = new Control[Controls.Count]; 

if (HasControls()) { 
    Controls.CopyTo(currentControls, 0); 
    Controls.Clear(); 
} 

// Misc work, add my controls, etc, e.g. 
Panel contentBox = new Panel { ID = "Content", CssClass = "content_border" }; 
// at some point, add wrapped controls to a control collection 
foreach (Control currentControl in currentControls) { 
    contentBox.Controls.Add(currentControl); 
} 

// Finally, add new controls back into Control collection 
Controls.Add(contentBox); 

而一直不俗表現。