Custom Control That Accepts Inline Code

G

Guest

I created a control which parses its sub objects and wraps the code in an
HTML wrapper. For example:

<abc:Section ID="Section1" Runat="server" Title="Example">
<p>This is an example.</p>
<p>This is an example.</p>
</abc:Section>

When the above example is rendered to HTML it appears as follows:

<table border=1 bordercolor=black>
<tr>
<th bgcolor=silver>Example</th>
<tr>
<tr>
<td>
<p>This is an example.</p>
<p>This is an example.</p>
</td>
</tr>
</table>

The problem I am experiencing is that placing a code render block results in
a "blank" section. I would expect the following section to be rendered as
above; however, it doesn't.

<abc:Section ID="Section1" Runat="server" Title="Example">
<%
for(int x=0; x<2; x++)
{
%>
<p>This is an example.</p>
<%
}
%>
</abc:Section>

Instead of rendering as expected, it renders as:

<table border=1 bordercolor=black>
<tr>
<th bgcolor=silver>Example</th>
<tr>
<tr>
<td>
</td>
</tr>
</table>

Any ideas on how to get this custom control to render inline code would be
greatly appreciated.
 
G

Guest

I figured it out.

The problem was in the Render method. When it came time to render the
subobjects I was doing this:

public override void Render(HtmlTextWriter writer)
{
...
foreach(Control control in this.Controls)
control.Render(writer);
...
}

Whenever a code block was included, this.Controls.Count was always 0, thus
never rendering properly. The solution was to change the section to:

public override void Render(HtmlTextWriter writer)
{
...
this.RenderChildren(writer);
...
}

This fixes the problem (and it is less code, woohoo).

Hope that helps somebody besides me.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top