<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Custom Server Controls</title><link>http://forums.asp.net/19.aspx</link><description>All about building ASP.NET server controls. &lt;a href="http://aspadvice.com/SignUp/list.aspx?l=12&amp;c=17" target="_blank"&gt;Email List&lt;/a&gt;</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: EnsureChildControls?</title><link>http://forums.asp.net/thread/3260973.aspx</link><pubDate>Fri, 26 Jun 2009 19:54:17 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3260973</guid><dc:creator>Brijesh50</dc:creator><author>Brijesh50</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3260973.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=19&amp;PostID=3260973</wfw:commentRss><description>&lt;p&gt;While reading an article on MSN for creating Custom Templated Control, the override method for DataBind calls CreateChildControls as follows&lt;/p&gt;&lt;p&gt;protected override CreateChildControls()&lt;/p&gt;&lt;p&gt;{&lt;/p&gt;&lt;p&gt;//Logic for creating TemplateContainer&lt;/p&gt;&lt;p&gt;//Logic for calling ITemplate.InstantiateIn()&lt;/p&gt;&lt;p&gt;//Logic for adding TemplateContainer to Controls Collection.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;protected override DataBind()&lt;/p&gt;&lt;p&gt;{&lt;/p&gt;&lt;p&gt;&amp;nbsp;CreateChildControls();&lt;/p&gt;&lt;p&gt;ChildControlsCreated&amp;nbsp; = true;&lt;/p&gt;&lt;p&gt;base.DataBind();&lt;br /&gt;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Another book I was reading did have the same logic for CreateChildControls but on databind does the following&lt;/p&gt;&lt;p&gt;protected override DataBind()&lt;/p&gt;&lt;p&gt;{&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; EnusreChildControls();&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; base.DataBind();&lt;br /&gt;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;I am confused as to whats the recommended practise?&lt;/p&gt;&lt;p&gt;Please Advise&lt;/p&gt;&lt;p&gt;Thanks&lt;/p&gt;&lt;p&gt;Brijesh&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: EnsureChildControls?</title><link>http://forums.asp.net/thread/1680434.aspx</link><pubDate>Tue, 24 Apr 2007 21:39:25 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:1680434</guid><dc:creator>scobrown</dc:creator><author>scobrown</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/1680434.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=19&amp;PostID=1680434</wfw:commentRss><description>I guess the short and sweet answer is that CreateChildControls() is not called by the constructor.</description></item><item><title>Re: EnsureChildControls?</title><link>http://forums.asp.net/thread/1680426.aspx</link><pubDate>Tue, 24 Apr 2007 21:36:01 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:1680426</guid><dc:creator>scobrown</dc:creator><author>scobrown</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/1680426.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=19&amp;PostID=1680426</wfw:commentRss><description>&lt;pre class=coloredcode&gt;&lt;span class=kwd&gt;Simple example: &lt;p&gt;Lets say you have class FooObject with a string property Bar, and the following property Foo in your Control:&amp;nbsp;&lt;p&gt;&amp;nbsp;&lt;pre class=coloredcode&gt;&lt;span class=kwd&gt;private&lt;/span&gt; FooObject _foo;
&lt;span class=kwd&gt;public string&lt;/span&gt; Foo
{
    &lt;span class=kwd&gt;get&lt;/span&gt;
    {
        &lt;span class=kwd&gt;return&lt;/span&gt; _foo.Bar;
    }
    &lt;span class=kwd&gt;set&lt;/span&gt;
    {
        _foo.Bar = &lt;span class=kwd&gt;value&lt;/span&gt;;
    }
}&lt;/pre&gt;&lt;/p&gt;&lt;/p&gt;&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;If you initialize _foo in CreateChildControls, then this code could throw a null reference error.&amp;nbsp; The problem would occur if you try to set the value of Foo in the Designer:&lt;/p&gt;
&lt;p&gt;&amp;lt;YourControl runat="server" id="newControl" Foo="This will cause a null reference" /&amp;gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Properties set in the Designer will be set at runtime before any calls to CreateChildControls.&amp;nbsp; You could get lucky if some other property gets set before yours and it calls CreateChildControls.&amp;nbsp; Imagine the .net runtime making the following series of call based on the above:&lt;/p&gt;
&lt;p&gt;YourControl newControl = &lt;span class=kwd&gt;new&lt;/span&gt; YourControl();&lt;br /&gt;newControl.Foo = &lt;span class=st&gt;"This will cause a null reference"&lt;/span&gt;;&lt;br /&gt;ParentControl.Controls.Add(newControl);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Anytime you are initializing an object in CreateChildControls, you should wrap it's property accessor with EnsureChildControls.&amp;nbsp; Even if you do the following:&lt;/p&gt;
&lt;p&gt;&lt;span class=kwd&gt;private&lt;/span&gt; FooObject _foo = new FooObject();&lt;br /&gt;&lt;span class=kwd&gt;public string&lt;/span&gt; Foo&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class=kwd&gt;get&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class=kwd&gt;return&lt;/span&gt; _foo.Bar;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class=kwd&gt;set&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _foo.Bar = &lt;span class=kwd&gt;value&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you set _foo to a new value in CreateChildControls(), the new value will have the default Bar value and&amp;nbsp;not the new one.&amp;nbsp; When creating a control, most properties you expose will operate on private variables whose value will be set in CreateChildControls(), so you should always add the EnsureChildControls.&lt;/p&gt;</description></item><item><title>EnsureChildControls?</title><link>http://forums.asp.net/thread/1679874.aspx</link><pubDate>Tue, 24 Apr 2007 15:02:40 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:1679874</guid><dc:creator>nisarkhan</dc:creator><author>nisarkhan</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/1679874.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=19&amp;PostID=1679874</wfw:commentRss><description>&lt;p&gt;Could somebody give me a better feel for the why's and when's of EnsureChildControls?&lt;br /&gt;&lt;br /&gt;I understand what it does ( calls CreateChildControls if needed ) , but I don't get when or why I would call it.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;what's the senario?&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;</description></item></channel></rss>