2014-05-01 71 views
0

工作,在關於解決方案: ASP.Net: User control with content area, it's clearly possible but I need some details與內容模板ASP.net自定義控件並沒有爲我

我試圖做同樣的,這裏是我的代碼:

控制代碼背後:

[ParseChildren(true, "Content")] 
[PersistChildren(false)] 
public partial class SlidingPanelControl : System.Web.UI.UserControl 
{ 
    protected override void OnInit(EventArgs e) 
    { 
     phContent.Controls.Add((Control)_content); 
     base.OnInit(e); 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
    } 

    private PlaceHolder _content; 
    [PersistenceMode(PersistenceMode.InnerProperty)] 
    public PlaceHolder Content { get { return _content; } set { _content = value; } } 
} 

而且控制ASPX:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SlidingPanelControl.ascx.cs" Inherits="Photography.Controls.MbExtruder.SlidingPanelControl" %> 
<div> 
<asp:Panel ID="pnlLockable" runat="server" Visible="False"> 
    <asp:PlaceHolder runat="server" ID="phContent" /> 
</asp:Panel> 
</div> 

這是我如何用我的主要頁面的控制:

<uc1:SlidingPanelControl runat="server" ID="SlidingPanelControl" 
    Title="About" Position="right" Opacity="1" WidthInPixels="600"> 
    <Content><h1>hello world</h1></Content> 
</uc1:SlidingPanelControl> 

這並沒有爲我工作,也沒有呈現HTML到控制佔位符。雖然當我調試控件的OnInit時,我可以看到_content控件包含我設置的所有html(即本例中的<h1>Hello World</h1>

任何猜測我在做什麼錯?

感謝

回答

0

好了,我檢查我的電腦上,我發現解決方案的實際工作:

[ParseChildren(false)] 
[PersistChildren(false)] 
public partial class WebUserControl1 : System.Web.UI.UserControl 
{ 
    protected override void OnInit(EventArgs e) 
    { 
     base.OnInit(e); 
     if (Content != null) 
     { 
      ContentContainer container = new ContentContainer(); 
      Content.InstantiateIn(container); 
      phContent.Controls.Add(container); 
     } 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    private ITemplate _content; 
    [PersistenceMode(PersistenceMode.InnerDefaultProperty), 
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
    TemplateInstance(TemplateInstance.Single)] 
    public ITemplate Content 
    { 
     get 
     { 
      return _content; 
     } 
     set 
     { 
      _content = value; 
     } 
    } 
} 

public class ContentContainer : Control, INamingContainer 
{ 
} 

我檢查自己的工作就像一個魅力。其他來源的代碼與您的代碼相同。

+0

嘗試兩個:(仍然相同的結果 – Bishoy

+0

@Bishoy編輯我的答案與新的解決方案 – Lesmian

相關問題