Controls present inside ITemplate gives error when accessed in PageLoad eventhttp://forums.asp.net/t/1717395.aspx/1?Controls+present+inside+ITemplate+gives+error+when+accessed+in+PageLoad+eventFri, 09 Sep 2011 04:52:16 -040017173954583522http://forums.asp.net/p/1717395/4583522.aspx/1?Controls+present+inside+ITemplate+gives+error+when+accessed+in+PageLoad+eventControls present inside ITemplate gives error when accessed in PageLoad event <p>Hello,</p> <p>I am creating a templated control. Only Single instance of Template is expected in the control. If I place any control inside template and try to access its property in PageLoad event, it gives an error - &quot;Object not set to an instance of an object.&quot; However, if I try to access the same control(which is present inside ITemplate) property in any event, it works fine returning the correct values.</p> <p>Code for control, ASPX &amp; code-behind is as follows:</p> <p><strong>Control Code</strong></p> <pre class="prettyprint">Namespace MyNS &lt;AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal), _ AspNetHostingPermission(SecurityAction.InheritanceDemand, Level:=AspNetHostingPermissionLevel.Minimal), _ ToolboxItem(False)&gt; _ Public Class GenericTemplateOwner Inherits Control Implements INamingContainer End Class &lt;AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal), _ AspNetHostingPermission(SecurityAction.InheritanceDemand, Level:=AspNetHostingPermissionLevel.Minimal), _ ToolboxData(&quot;&lt;{0}:MyControl runat=server&gt;&lt;/{0}:MyControl&gt;&quot;), _ ParseChildren(True), PersistChildren(False)&gt; _ Public Class MyControl Inherits Control Implements INamingContainer Private _template As ITemplate Private _owner As GenericTemplateOwner &lt;Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)&gt; _ Public ReadOnly Property ContentTemplateOwner() As GenericTemplateOwner Get EnsureChildControls() Return _owner End Get End Property &lt;Browsable(False), PersistenceMode(PersistenceMode.InnerProperty), DefaultValue(GetType(ITemplate), &quot;&quot;), _ TemplateInstance(TemplateInstance.Single), Description(&quot;Control template&quot;), _ TemplateContainer(GetType(GenericTemplateOwner)), RefreshProperties(RefreshProperties.All)&gt; _ Public Overridable Property Content() As ITemplate Get Return _template End Get Set(ByVal value As ITemplate) _template = value End Set End Property Protected Overrides Sub CreateChildControls() Me.Controls.Clear() 'Rendering the content of the Template _owner = New GenericTemplateOwner() If (Not Me.Content Is Nothing) Then Me.Content.InstantiateIn(Me.ContentTemplateOwner) End If Me.Controls.Add(Me.ContentTemplateOwner) End Sub Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter) If (Not Me.Page Is Nothing) Then Me.Page.VerifyRenderingInServerForm(Me) End If Dim tStr As New StringBuilder() tStr.Append(&quot;&lt;div id=&quot;&quot;&quot; &amp; Me.ClientID &amp; &quot;&quot;&quot; class=&quot;&quot;MyControl&quot;&quot;&gt;&quot;) writer.Write(tStr.ToString()) MyBase.Render(writer) writer.Write(&quot;&lt;/div&gt;&quot;) End Sub End Class End Namespace</pre> <p><strong>ASPX Code</strong></p> <pre class="prettyprint"> &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;cc1:MyControl ID="MyControl1" runat="server"&gt; &lt;Content&gt; &lt;asp:Button ID="Button1" runat="server" Text="Button" /&gt; &lt;/Content&gt; &lt;/cc1:MyControl&gt; &lt;/div&gt; &lt;/form&gt;</pre> <p><strong>Code-Behind Code</strong></p> <pre class="prettyprint">Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 'Works fine here Dim str As String = Button1.Text End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'Gives an Exception - "Object reference not set to an instance of an object." Dim str As String = Button1.Text End Sub End Class</pre> <p>Kindly suggest a workaround.</p> 2011-09-05T12:56:15-04:004585721http://forums.asp.net/p/1717395/4585721.aspx/1?Re+Controls+present+inside+ITemplate+gives+error+when+accessed+in+PageLoad+eventRe: Controls present inside ITemplate gives error when accessed in PageLoad event <p>Hello,</p> <p>The reason is that CreateChildrenControls won't be automatically called, And you should change your codes like:</p> <pre style="background:white; color:black; font-family:NSimSun"><span class="keyword" style="color:blue">Protected</span>&nbsp;<span class="keyword" style="color:blue">Sub</span>&nbsp;<span class="identifier">Page_Load</span>(<span class="keyword" style="color:blue">ByVal</span>&nbsp;<span class="identifier">sender</span>&nbsp;<span class="keyword" style="color:blue">As</span>&nbsp;<span class="keyword" style="color:blue">Object</span>,&nbsp;<span class="keyword" style="color:blue">ByVal</span>&nbsp;<span class="identifier">e</span>&nbsp;<span class="keyword" style="color:blue">As</span>&nbsp;<span class="identifier">System</span>.<span class="VB USER TYPES - IDENTIFIER - (TRANSIENT)" style="color:#2b91af">EventArgs</span>)&nbsp;<span class="keyword" style="color:blue">Handles</span>&nbsp;<span class="keyword" style="color:blue">Me</span>.<span class="identifier">Load</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><span class="keyword" style="color:blue">Dim</span>&nbsp;<span class="identifier">j</span>&nbsp;=&nbsp;<span class="identifier">MyControl1</span>.<span class="identifier">ContentTemplateOwner 'Call &nbsp;EnsureChildControls()<br> </span></strong><span class="identifier">Response</span>.<span class="identifier">Write</span>(<span class="identifier">Button1</span>.<span class="identifier">Text</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color:blue">End</span>&nbsp;<span class="keyword" style="color:blue">Sub</span></pre> 2011-09-07T01:51:24-04:004589432http://forums.asp.net/p/1717395/4589432.aspx/1?Re+Controls+present+inside+ITemplate+gives+error+when+accessed+in+PageLoad+eventRe: Controls present inside ITemplate gives error when accessed in PageLoad event <p>Thanks Decker.</p> <p>You suggestion worked. So, in the control code, I override OnInit() and put EnsureChildControls() in it. Now control always works like a charm.</p> <p>You rock.</p> 2011-09-09T04:52:16-04:00