By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,650 Members | 1,996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,650 IT Pros & Developers. It's quick & easy.

Literal control rendering empty

P: n/a
Hi all,

I have a custom control with an overridden Render method. Inside this
method I'm rendering each control in its collection using their
RenderControl method. However, I'm running into a problem in this
scenario:

<myprefix:mycontrol runat="server">
<%= SomeVariable %>
</myprefix:mycontrol>

The contained control is showing up in the control collection as a
LiteralControl, but its Text property is empty. Well, it's a series of
spaces, so it's not showing up with any content. I can put <%=
SomeVariable %anywhere outside my custom control and it will render
the variable value correctly.

Is there anything I can do to ensure the variable value is rendered?

I hope this post makes sense!

Thanks,

Paul

Jul 30 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Paul,

Iterating the Control collection manually and calling RenderChildren will not work with the <%=%tags as literal content, as you've
discovered on your own, but why would you want to do all that work when you can just call one method anyway?

Calling RenderChildren (or RenderContents instead if your class derives from WebControl) will render all child Controls for you. If
you override Render and do not call RenderBeginTag and RenderEndTag your control will not be able to use the ViewState or post-back
events by default since there will be no tag associated with your Control. The Render method essentially does the following:

RenderBeginTag();
RenderContents();
RenderEndTag();

So, you'll probably want to make sure that you don't override this default behavior. Call base.Render(), which will also render
your child Controls. Better yet, don't override Render if you only want to modify the contents of your control. Instead, override
RenderContents as such:

protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("This text will appear after the control's beginning tag and before the control's content and ending tag.");

// don't forget to call the base implementation which renders all child controls for you
base.RenderContents(writer);
}

Just for the sake of completeness, to allow literal content you must use the ParseChildrenAttribute on your class declaration:

[ParseChildren(false)]
public class MyControl : WebControl {}

Now you can use your control as such:

<test:MyControl runat="server" id="ctrl1">
<asp:Button runat="server" id="btn1" Text="Click Me!" />
<%= "Literal Content" %>
<asp:Label runat="server" id="lbl1">Label Text</asp:Label>
</test:MyControl>

.... and the output would be something like this:

<span id="ctrl1">
This text will appear after the control's beginning tag and before the control's content and ending tag.
<input type="submit" value="Click Me!" name="btn1" id="btn1" />
Literal Content
<span id="lbl1">Label Text</span>
</span>

--
Dave Sexton

<pa*********@gmail.comwrote in message news:11*********************@h48g2000cwc.googlegro ups.com...
Hi all,

I have a custom control with an overridden Render method. Inside this
method I'm rendering each control in its collection using their
RenderControl method. However, I'm running into a problem in this
scenario:

<myprefix:mycontrol runat="server">
<%= SomeVariable %>
</myprefix:mycontrol>

The contained control is showing up in the control collection as a
LiteralControl, but its Text property is empty. Well, it's a series of
spaces, so it's not showing up with any content. I can put <%=
SomeVariable %anywhere outside my custom control and it will render
the variable value correctly.

Is there anything I can do to ensure the variable value is rendered?

I hope this post makes sense!

Thanks,

Paul

Jul 31 '06 #2

P: n/a
Hi Dave,

Thanks for your reply. I'm rendering each control manually because, for
the sake of code economy, my custom control has form inputs and
validators as child controls, and it overrides the Render method to
sort out the layout of the child controls - i.e. the form input
elements and validator controls go in separate cells, and there is
other layout logic dependent on some custom attributes. I'm using the
HtmlTextWriter object in the Render method to write out the extra
layout elements, and am rendering the child controls in the appropriate
places in this method.

Will overriding RenderChildren or RenderContents allow me to insert
layout/formatting elements around the child controls?

Or, would I need to insert the extra elements into the Controls
collection, without using the writer to write them out in the Render
method?

Thanks again,

Paul

Dave Sexton wrote:
Hi Paul,

Iterating the Control collection manually and calling RenderChildren will not work with the <%=%tags as literal content, as you've
discovered on your own, but why would you want to do all that work when you can just call one method anyway?

Calling RenderChildren (or RenderContents instead if your class derives from WebControl) will render all child Controls for you. If
you override Render and do not call RenderBeginTag and RenderEndTag your control will not be able to use the ViewState or post-back
events by default since there will be no tag associated with your Control. The Render method essentially does the following:

RenderBeginTag();
RenderContents();
RenderEndTag();

So, you'll probably want to make sure that you don't override this default behavior. Call base.Render(), which will also render
your child Controls. Better yet, don't override Render if you only want to modify the contents of your control. Instead, override
RenderContents as such:

protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("This text will appear after the control's beginning tag and before the control's content and ending tag.");

// don't forget to call the base implementation which renders all child controls for you
base.RenderContents(writer);
}

Just for the sake of completeness, to allow literal content you must use the ParseChildrenAttribute on your class declaration:

[ParseChildren(false)]
public class MyControl : WebControl {}

Now you can use your control as such:

<test:MyControl runat="server" id="ctrl1">
<asp:Button runat="server" id="btn1" Text="Click Me!" />
<%= "Literal Content" %>
<asp:Label runat="server" id="lbl1">Label Text</asp:Label>
</test:MyControl>

... and the output would be something like this:

<span id="ctrl1">
This text will appear after the control's beginning tag and before the control's content and ending tag.
<input type="submit" value="Click Me!" name="btn1" id="btn1" />
Literal Content
<span id="lbl1">Label Text</span>
</span>

--
Dave Sexton

<pa*********@gmail.comwrote in message news:11*********************@h48g2000cwc.googlegro ups.com...
Hi all,

I have a custom control with an overridden Render method. Inside this
method I'm rendering each control in its collection using their
RenderControl method. However, I'm running into a problem in this
scenario:

<myprefix:mycontrol runat="server">
<%= SomeVariable %>
</myprefix:mycontrol>

The contained control is showing up in the control collection as a
LiteralControl, but its Text property is empty. Well, it's a series of
spaces, so it's not showing up with any content. I can put <%=
SomeVariable %anywhere outside my custom control and it will render
the variable value correctly.

Is there anything I can do to ensure the variable value is rendered?

I hope this post makes sense!

Thanks,

Paul
Jul 31 '06 #3

P: n/a
Hi Paul,

For some reason this thread wasn't being watched, but I found it once again.

If you can use a UserControl instead that might be more appropriate. If you still require the use of a custom control then you
might try creating a composite WebControl. In the OnInit event of your class you could add an HtmlTable to the Controls collection
for laying out the other children. In that case you wouldn't have to render the child controls manually but you could still
participate in the rendering of your control to add extra functionality but don't forget to call the base implementations' of the
methods you override.
Will overriding RenderChildren or RenderContents allow me to insert
layout/formatting elements around the child controls?
Yes, however this might affect post-backs with regards to events and data (ViewState) if you use other Controls for layout because
the control heirarchy will differ between when your control is initialized on post-back and when it was rendered. In this case, if
any of the parenting controls implement INamingContainer you'll have invalid control names, therefore ASP.NET will not be able to
initialize the controls properly. You should only add new Controls to the collection before the Load event of your control
(preferably overriding OnInit) . If you want to layout your controls using methods of the HtmlTextWriter then you shouldn't have a
problem other than it's more difficult than using controls, IMO.
Or, would I need to insert the extra elements into the Controls
collection, without using the writer to write them out in the Render
method?
Read above.

--
Dave Sexton

<pa*********@gmail.comwrote in message news:11*********************@m73g2000cwd.googlegro ups.com...
Hi Dave,

Thanks for your reply. I'm rendering each control manually because, for
the sake of code economy, my custom control has form inputs and
validators as child controls, and it overrides the Render method to
sort out the layout of the child controls - i.e. the form input
elements and validator controls go in separate cells, and there is
other layout logic dependent on some custom attributes. I'm using the
HtmlTextWriter object in the Render method to write out the extra
layout elements, and am rendering the child controls in the appropriate
places in this method.

Will overriding RenderChildren or RenderContents allow me to insert
layout/formatting elements around the child controls?

Or, would I need to insert the extra elements into the Controls
collection, without using the writer to write them out in the Render
method?

Thanks again,

Paul

Dave Sexton wrote:
>Hi Paul,

Iterating the Control collection manually and calling RenderChildren will not work with the <%=%tags as literal content, as
you've
discovered on your own, but why would you want to do all that work when you can just call one method anyway?

Calling RenderChildren (or RenderContents instead if your class derives from WebControl) will render all child Controls for you.
If
you override Render and do not call RenderBeginTag and RenderEndTag your control will not be able to use the ViewState or
post-back
events by default since there will be no tag associated with your Control. The Render method essentially does the following:

RenderBeginTag();
RenderContents();
RenderEndTag();

So, you'll probably want to make sure that you don't override this default behavior. Call base.Render(), which will also render
your child Controls. Better yet, don't override Render if you only want to modify the contents of your control. Instead,
override
RenderContents as such:

protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("This text will appear after the control's beginning tag and before the control's content and ending tag.");

// don't forget to call the base implementation which renders all child controls for you
base.RenderContents(writer);
}

Just for the sake of completeness, to allow literal content you must use the ParseChildrenAttribute on your class declaration:

[ParseChildren(false)]
public class MyControl : WebControl {}

Now you can use your control as such:

<test:MyControl runat="server" id="ctrl1">
<asp:Button runat="server" id="btn1" Text="Click Me!" />
<%= "Literal Content" %>
<asp:Label runat="server" id="lbl1">Label Text</asp:Label>
</test:MyControl>

... and the output would be something like this:

<span id="ctrl1">
This text will appear after the control's beginning tag and before the control's content and ending tag.
<input type="submit" value="Click Me!" name="btn1" id="btn1" />
Literal Content
<span id="lbl1">Label Text</span>
</span>

--
Dave Sexton

<pa*********@gmail.comwrote in message news:11*********************@h48g2000cwc.googlegro ups.com...
Hi all,

I have a custom control with an overridden Render method. Inside this
method I'm rendering each control in its collection using their
RenderControl method. However, I'm running into a problem in this
scenario:

<myprefix:mycontrol runat="server">
<%= SomeVariable %>
</myprefix:mycontrol>

The contained control is showing up in the control collection as a
LiteralControl, but its Text property is empty. Well, it's a series of
spaces, so it's not showing up with any content. I can put <%=
SomeVariable %anywhere outside my custom control and it will render
the variable value correctly.

Is there anything I can do to ensure the variable value is rendered?

I hope this post makes sense!

Thanks,

Paul

Aug 1 '06 #4

P: n/a
Thanks Dave,

Which rendering/control creation method would preserve the literal
content (i.e. <%= MyVariable %>) contained in my control? This is the
main problem for me right now, and the combination of overriding Render
and using each controls RenderControl method is failing for me right
now.

Thanks,

Paul

Dave Sexton wrote:
Hi Paul,

For some reason this thread wasn't being watched, but I found it once again.

If you can use a UserControl instead that might be more appropriate. If you still require the use of a custom control then you
might try creating a composite WebControl. In the OnInit event of your class you could add an HtmlTable to the Controls collection
for laying out the other children. In that case you wouldn't have to render the child controls manually but you could still
participate in the rendering of your control to add extra functionality but don't forget to call the base implementations' of the
methods you override.
Will overriding RenderChildren or RenderContents allow me to insert
layout/formatting elements around the child controls?

Yes, however this might affect post-backs with regards to events and data (ViewState) if you use other Controls for layout because
the control heirarchy will differ between when your control is initialized on post-back and when it was rendered. In this case, if
any of the parenting controls implement INamingContainer you'll have invalid control names, therefore ASP.NET will not be able to
initialize the controls properly. You should only add new Controls to the collection before the Load event of your control
(preferably overriding OnInit) . If you want to layout your controls using methods of the HtmlTextWriter then you shouldn't have a
problem other than it's more difficult than using controls, IMO.
Or, would I need to insert the extra elements into the Controls
collection, without using the writer to write them out in the Render
method?

Read above.

--
Dave Sexton

<pa*********@gmail.comwrote in message news:11*********************@m73g2000cwd.googlegro ups.com...
Hi Dave,

Thanks for your reply. I'm rendering each control manually because, for
the sake of code economy, my custom control has form inputs and
validators as child controls, and it overrides the Render method to
sort out the layout of the child controls - i.e. the form input
elements and validator controls go in separate cells, and there is
other layout logic dependent on some custom attributes. I'm using the
HtmlTextWriter object in the Render method to write out the extra
layout elements, and am rendering the child controls in the appropriate
places in this method.

Will overriding RenderChildren or RenderContents allow me to insert
layout/formatting elements around the child controls?

Or, would I need to insert the extra elements into the Controls
collection, without using the writer to write them out in the Render
method?

Thanks again,

Paul

Dave Sexton wrote:
Hi Paul,

Iterating the Control collection manually and calling RenderChildren will not work with the <%=%tags as literal content, as
you've
discovered on your own, but why would you want to do all that work when you can just call one method anyway?

Calling RenderChildren (or RenderContents instead if your class derives from WebControl) will render all child Controls for you.
If
you override Render and do not call RenderBeginTag and RenderEndTag your control will not be able to use the ViewState or
post-back
events by default since there will be no tag associated with your Control. The Render method essentially does the following:

RenderBeginTag();
RenderContents();
RenderEndTag();

So, you'll probably want to make sure that you don't override this default behavior. Call base.Render(), which will also render
your child Controls. Better yet, don't override Render if you only want to modify the contents of your control. Instead,
override
RenderContents as such:

protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("This text will appear after the control's beginning tag and before the control's content and ending tag.");

// don't forget to call the base implementation which renders all child controls for you
base.RenderContents(writer);
}

Just for the sake of completeness, to allow literal content you must use the ParseChildrenAttribute on your class declaration:

[ParseChildren(false)]
public class MyControl : WebControl {}

Now you can use your control as such:

<test:MyControl runat="server" id="ctrl1">
<asp:Button runat="server" id="btn1" Text="Click Me!" />
<%= "Literal Content" %>
<asp:Label runat="server" id="lbl1">Label Text</asp:Label>
</test:MyControl>

... and the output would be something like this:

<span id="ctrl1">
This text will appear after the control's beginning tag and before the control's content and ending tag.
<input type="submit" value="Click Me!" name="btn1" id="btn1" />
Literal Content
<span id="lbl1">Label Text</span>
</span>

--
Dave Sexton

<pa*********@gmail.comwrote in message news:11*********************@h48g2000cwc.googlegro ups.com...
Hi all,

I have a custom control with an overridden Render method. Inside this
method I'm rendering each control in its collection using their
RenderControl method. However, I'm running into a problem in this
scenario:

<myprefix:mycontrol runat="server">
<%= SomeVariable %>
</myprefix:mycontrol>

The contained control is showing up in the control collection as a
LiteralControl, but its Text property is empty. Well, it's a series of
spaces, so it's not showing up with any content. I can put <%=
SomeVariable %anywhere outside my custom control and it will render
the variable value correctly.

Is there anything I can do to ensure the variable value is rendered?

I hope this post makes sense!

Thanks,

Paul
Aug 1 '06 #5

P: n/a
Hi Paul,

The reason <%=%renders nothing within your control's tags is because your calling each control's RenderControl method manually
instead of allowing the base class to do the work for you. What it comes down to is that you have to ensure that RenderChildren
gets called at some point.

So, to answer your question, anything I've suggested will work. Just make sure that you use the base functionality to render
children at some point by calling the base implementation of any Render, RenderContents or RenderChildren method that you override
or use a Control hierarchy for layout instead of overriding any of the "Render" methods and initialize the controls in a Page.Init
event handler.

HTH

--
Dave Sexton

<pa*********@gmail.comwrote in message news:11**********************@75g2000cwc.googlegro ups.com...
Thanks Dave,

Which rendering/control creation method would preserve the literal
content (i.e. <%= MyVariable %>) contained in my control? This is the
main problem for me right now, and the combination of overriding Render
and using each controls RenderControl method is failing for me right
now.

Thanks,

Paul

Dave Sexton wrote:
>Hi Paul,

For some reason this thread wasn't being watched, but I found it once again.

If you can use a UserControl instead that might be more appropriate. If you still require the use of a custom control then you
might try creating a composite WebControl. In the OnInit event of your class you could add an HtmlTable to the Controls
collection
for laying out the other children. In that case you wouldn't have to render the child controls manually but you could still
participate in the rendering of your control to add extra functionality but don't forget to call the base implementations' of the
methods you override.
Will overriding RenderChildren or RenderContents allow me to insert
layout/formatting elements around the child controls?

Yes, however this might affect post-backs with regards to events and data (ViewState) if you use other Controls for layout
because
the control heirarchy will differ between when your control is initialized on post-back and when it was rendered. In this case,
if
any of the parenting controls implement INamingContainer you'll have invalid control names, therefore ASP.NET will not be able to
initialize the controls properly. You should only add new Controls to the collection before the Load event of your control
(preferably overriding OnInit) . If you want to layout your controls using methods of the HtmlTextWriter then you shouldn't have
a
problem other than it's more difficult than using controls, IMO.
Or, would I need to insert the extra elements into the Controls
collection, without using the writer to write them out in the Render
method?

Read above.

--
Dave Sexton

<pa*********@gmail.comwrote in message news:11*********************@m73g2000cwd.googlegro ups.com...
Hi Dave,

Thanks for your reply. I'm rendering each control manually because, for
the sake of code economy, my custom control has form inputs and
validators as child controls, and it overrides the Render method to
sort out the layout of the child controls - i.e. the form input
elements and validator controls go in separate cells, and there is
other layout logic dependent on some custom attributes. I'm using the
HtmlTextWriter object in the Render method to write out the extra
layout elements, and am rendering the child controls in the appropriate
places in this method.

Will overriding RenderChildren or RenderContents allow me to insert
layout/formatting elements around the child controls?

Or, would I need to insert the extra elements into the Controls
collection, without using the writer to write them out in the Render
method?

Thanks again,

Paul

Dave Sexton wrote:
Hi Paul,

Iterating the Control collection manually and calling RenderChildren will not work with the <%=%tags as literal content, as
you've
discovered on your own, but why would you want to do all that work when you can just call one method anyway?

Calling RenderChildren (or RenderContents instead if your class derives from WebControl) will render all child Controls for
you.
If
you override Render and do not call RenderBeginTag and RenderEndTag your control will not be able to use the ViewState or
post-back
events by default since there will be no tag associated with your Control. The Render method essentially does the following:

RenderBeginTag();
RenderContents();
RenderEndTag();

So, you'll probably want to make sure that you don't override this default behavior. Call base.Render(), which will also
render
your child Controls. Better yet, don't override Render if you only want to modify the contents of your control. Instead,
override
RenderContents as such:

protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("This text will appear after the control's beginning tag and before the control's content and ending tag.");

// don't forget to call the base implementation which renders all child controls for you
base.RenderContents(writer);
}

Just for the sake of completeness, to allow literal content you must use the ParseChildrenAttribute on your class declaration:

[ParseChildren(false)]
public class MyControl : WebControl {}

Now you can use your control as such:

<test:MyControl runat="server" id="ctrl1">
<asp:Button runat="server" id="btn1" Text="Click Me!" />
<%= "Literal Content" %>
<asp:Label runat="server" id="lbl1">Label Text</asp:Label>
</test:MyControl>

... and the output would be something like this:

<span id="ctrl1">
This text will appear after the control's beginning tag and before the control's content and ending tag.
<input type="submit" value="Click Me!" name="btn1" id="btn1" />
Literal Content
<span id="lbl1">Label Text</span>
</span>

--
Dave Sexton

<pa*********@gmail.comwrote in message news:11*********************@h48g2000cwc.googlegro ups.com...
Hi all,

I have a custom control with an overridden Render method. Inside this
method I'm rendering each control in its collection using their
RenderControl method. However, I'm running into a problem in this
scenario:

<myprefix:mycontrol runat="server">
<%= SomeVariable %>
</myprefix:mycontrol>

The contained control is showing up in the control collection as a
LiteralControl, but its Text property is empty. Well, it's a series of
spaces, so it's not showing up with any content. I can put <%=
SomeVariable %anywhere outside my custom control and it will render
the variable value correctly.

Is there anything I can do to ensure the variable value is rendered?

I hope this post makes sense!

Thanks,

Paul


Aug 1 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.