title: How can I render children tags in a custom server control?
body: <p>I'm working on a custom ServerControl, I've created it like below :<br /></p>
  
  <pre><code>[ParseChildren(true), PersistChildren(true)]
  [ToolboxData("&lt;{0}:Menu runat=\"server\"&gt;&lt;/{0}:Menu&gt;")]
  public class Menu : WebControl
  { 
      .....
  
      [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
      [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
      public MenuItem MenuItems { get; set; }
  }
  
  [ParseChildren(true), PersistChildren(true)]
  public class MenuItem : WebControl
  { 
      ...... 
  
      [PersistenceMode(PersistenceMode.InnerProperty)]
      public MenuItem SubMenuItems
      {
          get
          {
              if (_SubMenuItems == null) return new MenuItem();
              return _SubMenuItems;
          }
          set
          {
              _SubMenuItems = value;
          }
      }
      private MenuItem _SubMenuItems;
  
      [TemplateContainer(typeof(MenuItem))]
      [PersistenceMode(PersistenceMode.InnerProperty)]
      public ITemplate Template { get; set; }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>&lt;%@ Register Assembly="JQueryMenu" Namespace="JQueryMenu" TagPrefix="MdsMenu" %&gt;
  &lt;MdsMenu:Menu ID="Menu1" runat="server"&gt;
      &lt;AnimationItems AnimationSpeed="Fast" AnimationType="Opacity_Height" DropShadow="true"
          Delay="1000" /&gt;
      &lt;MdsMenu:MenuItem ID="MenuItem1" runat="server" Text="MenuItem 01"&gt;
          &lt;MdsMenu:MenuItem runat="server"&gt;
              &lt;Template&gt;
                  &lt;asp:Button ID="Button1" runat="server" Text="Button" /&gt;
                  &lt;asp:CheckBox ID="CheckBox1" runat="server" /&gt;
              &lt;/Template&gt;
          &lt;/MdsMenu:MenuItem&gt;
      &lt;/MdsMenu:MenuItem&gt;
      &lt;MdsMenu:MenuItem ID="MenuItem2" runat="server" Text="MenuItem 01"&gt;
          &lt;MdsMenu:MenuItem ID="MenuItem3" runat="server"&gt;
              &lt;Template&gt;
                  &lt;asp:Button ID="Button2" runat="server" Text="Button" /&gt;
                  &lt;asp:CheckBox ID="CheckBox2" runat="server" /&gt;
              &lt;/Template&gt;
              &lt;MdsMenu:MenuItem ID="MenuItem5" runat="server" Text="MenuItem 05"&gt;
              &lt;/MdsMenu:MenuItem&gt;
              &lt;MdsMenu:MenuItem ID="MenuItem6" runat="server" Text="MenuItem 06"&gt;
              &lt;/MdsMenu:MenuItem&gt;
              &lt;MdsMenu:MenuItem ID="MenuItem4" runat="server"&gt;
                  &lt;Template&gt;
                      &lt;asp:Image ID="Image1" runat="server" /&gt;
                      &lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt;
                  &lt;/Template&gt;
              &lt;/MdsMenu:MenuItem&gt;
          &lt;/MdsMenu:MenuItem&gt;
      &lt;/MdsMenu:MenuItem&gt;
  &lt;/MdsMenu:Menu&gt;
  </code></pre>
  
  <p>Now, How can I Parse it and render it in <code>RenderContent</code> method ? !!!
  The following method is always throw the first Exception, it means this.Controls is always empty !!! <br />How can I do it and how I can access to the nested children in <code>RenderControl</code> method ?</p>
  
  <pre><code>public class Menu : WebControl
  {
      ....
  
      public override void RenderControl(HtmlTextWriter output)
      { 
          if (!this.HasControls())
             throw new Exception("Controls are empty");
          ....
      }
  }
  </code></pre>