locked
Cannot render template contents RRS feed

  • Question

  • User-825949172 posted

    Please advice what is wrong with following code. Nothing is rendered from <HeaderTemplate>

    1    using System;
    2    using System.Collections.Generic;
    3    using System.ComponentModel;
    4    using System.Linq;
    5    using System.Text;
    6    using System.Web;
    7    using System.Web.UI;
    8    using System.Web.UI.WebControls;
    9    
    10   namespace AspNetServerControls
    11   {
    12   	[DefaultProperty("Text")]
    13   	[ToolboxData("&lt;{0}:MyGridControl runat=server></{0}:MyGridControl>")]
    14   	public class MyGridControl : CompositeControl
    15   	{
    16   		#region contructor
    17   
    18   		public MyGridControl()
    19   		{
    20   		}
    21   
    22   		#endregion
    23   
    24   		#region public properties
    25   
    26   		[TemplateContainer(typeof(BasicTemplateContainer)),
    27   		 PersistenceMode(PersistenceMode.InnerProperty),
    28   		 Browsable(false)]
    29   		public ITemplate HeaderTemplate
    30   		{
    31   			get;
    32   			set;
    33   		}
    34   
    35   		#endregion
    36   
    37   		#region overriden methods & properties
    38   
    39   		protected override void RenderContents(HtmlTextWriter output)
    40   		{
    41   			//output.Write(Text);
    42   		}
    43   
    44   		protected override void CreateChildControls()
    45   		{
    46   			Controls.Clear();
    47   
    48   			if (HeaderTemplate != null)
    49   			{
    50   				BasicTemplateContainer header = new BasicTemplateContainer();
    51   				HeaderTemplate.InstantiateIn(header);
    52   				Controls.Add(header);
    53   			}
    54   		}
    55   
    56   		public override void DataBind()
    57   		{
    58   			CreateChildControls();
    59   			ChildControlsCreated = true;
    60   
    61   			base.DataBind();
    62   		}
    63   
    64   		public override ControlCollection Controls
    65   		{
    66   			get
    67   			{
    68   				EnsureChildControls();
    69   				return base.Controls;
    70   			}
    71   		}
    72   
    73   		#endregion
    74   	}
    75   
    76   
    77   	public class BasicTemplateContainer : WebControl, INamingContainer
    78   	{
    79   		public BasicTemplateContainer() : base(HtmlTextWriterTag.Span) { }
    80   	}
    81   }
    82   
    
       
            <bhav:MyGridControl ID="grd" runat="server">
                <HeaderTemplate>My Template</HeaderTemplate>
            </bhav:MyGridControl>
     
    Wednesday, September 3, 2008 4:41 PM

Answers

  • User-825949172 posted

     I found the work around. Just need to either remove RenderContents method or call base.RenderContents(output); in the overriden method.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 4, 2008 4:43 AM

All replies

  • User-825949172 posted

     I found the work around. Just need to either remove RenderContents method or call base.RenderContents(output); in the overriden method.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 4, 2008 4:43 AM
  • User481221548 posted

    Hi there

    Don`t know the behaviour of the compositecontrol.
    On controls derive from Control / WebControl or something like this, i use OnInit fo template instantiation.

    Try this.

    edit:
    Jep, thats clear.
    RenderContents renders all the Content, so if its blank, only the Begin- and EndTag would be rendered.

    Thursday, September 4, 2008 7:41 AM