Tutorial :How do I render User Control children at a specific location?



Question:

I have a very simplistic user control that looks like this:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="wfWindow.ascx.cs" Inherits="webfanatix.co.za.wfWindow" %>  <div>Just a test...[x]</div>  

with this code behind:

[ParseChildren(false)]  [PersistChildren(true)]  public partial class wfWindow : System.Web.UI.UserControl  {      protected override void Render(HtmlTextWriter writer)      {          RenderChildren(writer);      }  }  

And the usage thereof looks like this:

<wf:wfWindow runat="server">This content should go where [x] is.</wf:wfWindow>  

I'm no ASP.NET pro, so how do I get the content to render exactly where the [x] appears in my user control?

RenderChildren is rendering my content, but it is only appended to the end of the UserControl output. I need it to go and sit right where [x] marks the spot.

Thanks in advance!


Solution:1

If you want the implementation to decide where to put the content, then look into templated user controls.

Otherwise, use a placeholder control and override the AddParsedSubObject method


Solution:2

Change [X] into a control of it's own that can hold other controls, such as a placeholder or a panel.

Then override AddParsedSubObject(). The default implementation of this just adds the controls to the usercontrol's Controls collection - which means they get added to the end. Override this to add them to your placeholder.

   protected override void AddParsedSubObject(Object obj)      {           PanelWhereXIs.Add(obj);     }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »