473,394 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Literal control rendering empty

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
5 2291
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just...
14
by: Joe | last post by:
Hello All: I am trying to dynamically populate a web page with literal content and controls (textboxes and checkboxes (and eventually two buttons - the buttons do not appear in the code yet). I...
5
by: George Jordanov Ivanov | last post by:
All, Today I was playing with Literal control. Basically it is more or less like the Label control with this difference that Literal control is something like "lightweight" text container (that...
2
by: Paul | last post by:
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.