Custom Control that accepts Inline Code

J

JWhitted

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.
 
J

JWhitted

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.
 
N

Nathan Wilkes

I was having a similar problem - for me, I had a special title control -
I wanted to render the contents between the tags using a text randomizer
routine.

<frame:Title runat="server" random="true">
Hi there <%=profile.name%>, welcome to our bullocks example.
</frame>

The above server control needs the contents of the tag so it can run a
case randomizer on it - the output is something like

Hi tHeRe AbbY. wELcoMe.. etc (it's for a kids site)

Originally, I was using a controlbuilder that allowed literal controls -
and overriding addparsedsubobject to add the contents of the literal
control to the "contents string".

As soon as you throw in an inline tag - boom, it dies.

Looking at the compiled page code - I see that a function is created
called __render<controlnamehere> - this basically looks like this
writer.Write ("Hi there");
writer.Write (profile.name);
writer.Write (", welcome to our bullocks example");

And so it dawned on me why there are no child controls etc - it's
because a custom rendering function is created (at the end of this, it
adds itself to the control as a render delegate)... When you call the
RenderChildren funciton - it's calling the delegate.

What you need to do (in this case) is create a custom HtmlWriter..
something like this:

StringWriter strWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter);
RenderChildren(htmlWriter);
string strMyContent = strWriter.ToString();
htmlWriter.close();
strWriter.close();

Hope that helps the next person who is a-hunting for these sorts of
problems.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top