rendering child controls in designer

J

johnny

I have been unable to get some of my custom server controls that have
children to render properly in the vs.net designer. I have gone
through other postings and tried their suggestions, but nothing has
worked. Below are a couple of simple controls I came up with to
illustrate my problem. Hopefully, someone can help me out.



[ParseChildren(false), PersistChildren(true)]

public class ChildControl : Control, INamingContainer

{

public override ControlCollection Controls

{

get

{

EnsureChildControls();

return base.Controls;

}

}



protected override void Render(HtmlTextWriter writer)

{

writer.Write("<tr><td>Hello");

base.Render (writer);

writer.Write("</td></tr>");

}

}



[ParseChildren(false), PersistChildren(true)]

public class ParentControl : Control, INamingContainer

{

public override ControlCollection Controls

{

get

{

EnsureChildControls();

return base.Controls;

}

}



protected override void Render(HtmlTextWriter writer)

{

writer.Write("<table>");

base.Render (writer);

writer.Write("</table>");

}


Then in the aspx file:



<form id="Form1" method="post" runat="server">

<Custom:parentControl id="p" runat="server">

<Custom:ChildControl id="c1" runat="server" />

<Custom:ChildControl id="c2" runat="server" />

</Custom:parentControl>

</form>



Thanks
 
J

johnny

threecrans said:
johnny said:
I have been unable to get some of my custom server controls that have
children to render properly in the vs.net designer. I have gone
through other postings and tried their suggestions, but nothing has
worked. Below are a couple of simple controls I came up with to
illustrate my problem. Hopefully, someone can help me out.



[ParseChildren(false), PersistChildren(true)]

public class ChildControl : Control, INamingContainer

{

public override ControlCollection Controls

{

get

{

EnsureChildControls();

return base.Controls;

}

}



protected override void Render(HtmlTextWriter writer)

{

writer.Write("<tr><td>Hello");

base.Render (writer);

writer.Write("</td></tr>");

}

}



[ParseChildren(false), PersistChildren(true)]

public class ParentControl : Control, INamingContainer

{

public override ControlCollection Controls

{

get

{

EnsureChildControls();

return base.Controls;

}

}



protected override void Render(HtmlTextWriter writer)

{

writer.Write("<table>");

base.Render (writer);

writer.Write("</table>");

}


Then in the aspx file:



<form id="Form1" method="post" runat="server">

<Custom:parentControl id="p" runat="server">

<Custom:ChildControl id="c1" runat="server" />

<Custom:ChildControl id="c2" runat="server" />

</Custom:parentControl>

</form>



Thanks

Create your own Designer which derives from Control Designer. Override the
GetDesignTimeHtml method. Access the Controls collection within this method
to force the child controls to be created. Something like this (borrowed
from Developing Microsoft ASP.NET Server Controls And Components book).

public class MyDesigner : ControlDesigner
{
public override string GetDesignTimeHtml()
{
ControlCollection controls = ((Control)Component).Controls;
return base.GetDesignTimeHTML();
}
}

then associate the designer with the control using an attribute

[ Designer(typeof(MyDesigner))]
public class ParentControl : Control, INamingContainer
Thank you for your response.

I did as you recommended, but it still didn't work. I added a
constructor to MyDesigner and set ReadOnly=false in it. That seemed
to let the children render. However, it still didn't render properly.
The HTML should produce a table with two rows, but the designer showed
the two children side by side.

Any more suggestions would be appreciated
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top