locked
Template DataBinder not working RRS feed

  • Question

  • User-825949172 posted

    In the following simple code I am trying to display LabelText property value using databinder in the header template but it seems not displaying. Please advice what is incorrect I am doing here.

     

     

    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   		public string LabelText
    36   		{
    37   			get;
    38   			set;
    39   		}
    40   
    41   		#endregion
    42   
    43   		#region overriden methods & properties
    44   
    45   		protected override void RenderContents(HtmlTextWriter output)
    46   		{
    47   			//output.Write(Text);
    48   			base.RenderContents(output);
    49   		}
    50   
    51   		protected override void CreateChildControls()
    52   		{
    53   			Controls.Clear();
    54   
    55   			if (HeaderTemplate != null)
    56   			{
    57   				BasicTemplateContainer header = new BasicTemplateContainer();
    58   				header.TemplateLabelText = this.LabelText;
    59   				HeaderTemplate.InstantiateIn(header);
    60   				Controls.Add(header);
    61   			}
    62   		}
    63   
    64   		public override void DataBind()
    65   		{
    66   			CreateChildControls();
    67   			ChildControlsCreated = true;
    68   
    69   			base.DataBind();
    70   		}
    71   
    72   		public override ControlCollection Controls
    73   		{
    74   			get
    75   			{
    76   				EnsureChildControls();
    77   				return base.Controls;
    78   			}
    79   		}
    80   
    81   		#endregion
    82   	}
    83   
    84   
    85   	public class BasicTemplateContainer : Control, INamingContainer
    86   	{
    87   		#region constructors
    88   
    89   		public BasicTemplateContainer() { }
    90   
    91   		#endregion
    92   
    93   		#region public properties
    94   
    95   		public string TemplateLabelText
    96   		{
    97   			get;
    98   			set;
    99   		}
    100  
    101  		#endregion
    102  	}
    103  }
    104  
    
       
    1            <bhav:MyGridControl ID="grd" LabelText="Hello World" runat="server">
    2                <HeaderTemplate><%# DataBinder.Eval(Container, "TemplateLabelText")%> from Bhavesh</HeaderTemplate>
    3            </bhav:MyGridControl>
    
     
    Wednesday, September 10, 2008 4:17 PM

Answers

  • User-825949172 posted

    Bizzarely you have to also call grid.Databind() in the PageLoad event.

    Not sure why? as most of the books on server controls do not specify this.

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