Re: Custom ContentPlaceHolder




On Fri, 26 Jun 2009 10:40:32 -0400, "gerry" <germ2@xxxxxxxxxxxxxxxx>
wrote:


"Registered User" <n4jvp@xxxxxxxxxxxxx> wrote in message
news:dc6845h9vsvegbtp53dffbs1tv42o8h9im@xxxxxxxxxx
On Thu, 25 Jun 2009 16:40:20 -0400, "gerry" <germ2@xxxxxxxxxxxxxxxx>
wrote:

"Registered User" <n4jvp@xxxxxxxxxxxxx> wrote in message
news:9of745ptpc3770b9b0l4avnho1n0bqtvuj@xxxxxxxxxx
On Thu, 25 Jun 2009 13:12:37 -0400, "gerry" <germ2@xxxxxxxxxxxxxxxx>
wrote:



although it is irrelevant to the discussion of this undocumented 'feature'
of the ContentPlaceHolder, in this particular case, the intent was to
override the Render method to capture the rendered content, but I'm sure
there are any number of valid reasons.

Can the content actually be captured in the derived class' overridden
Render method without infinite recursion?

why not - where do you see recursion in this ?

protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
base .Render(htw);

string content= sb.ToString();

writer.write(content);

// ... do whatever else with content
}

I was asking a question not saying there would be recursion.


The intern comment denigrates the efforts of everyone involved in the
design and production of the .NET framework.

The comment wasn't directed at the designers/developers of the .NET
framework, just the designer/developer of the web form designer , and if
the
shoe fits ...

There are many reviews between conceptualization and release. No one
individual or small group is given free rein to do what they think is
best with no oversight. There are constraints which are unknown to us
and hopefully these constraints will be designed out of future
versions of the tools. In the meantime you're painting with a broad
brush.

Some things slip through, especially when noone has thought to test for
them.
But more to the point, poor ( short-sighted ) design - case in point , the
framework classes that started out as either internal or sealed or both in
earlier releases that were eventually made public and/or unsealed because
there actually were valid reasons for it.


If the ContentPlaceHolder was intentionally meant to be unusable as a base
class then it should have been sealed.
It also worth noting that the custom ContentPlaceHolder control works just
fine at runtime, this a 100% designer issue.

Yes there is a designer issue and it has been acknowledged. Knowing
what you're trying to do makes the real question; is a derived
ContentPlaceHolder necessary to capture the content? Add an
OnPreRender event handler to a ContentPlaceHolder instance. Put the
code below into the handler and make the appropriate change to
control's ID.

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
ContentPlaceHolder1.RenderControl(htw);
string s = sb.ToString();


calling render before PreRender has completed or PreRenderComplete has
executed and knowing that PreRender will be called again in the proper
request event sequence sounds pretty hoky.
did you even try this ?
Yes and the OnPreRender event is only fired once.

I did - any server controls contained in the ContentPlaceHolder will barf
with a "must be placed inside a form tag with runat=server. " error - not
surprising.
I did not experience any problems with this using VS2008.

My guess is that any databound controls would also have problems with being
rendered twice - that is if they could get past issue just stated.

I did not try databound controls so I won't make any guesses about
rendering the same databound control twice, each time to a different
stream. What impact to the databinding is expected when a control is
rendered twice to different streams?

Admittedly the OnPreRender event may not be the most suitable
location. The same code could be placed in the master page's Page_Load
method. In the example shown at the link below the databound control
gets rendered twice with no issues.
http://www.4guysfromrolla.com/articles/091102-1.aspx


The string s will contain the ContentPlaceHolder's content.

No it won't.

I did try it using server controls and the string was as expected with
no errors.

If the content can be captured without using a derived class, does the
valid
reason to derive a class still exist?

Who knows, I don't pretend to know every possible reason for deriving such a
custom control.

Re-read the the sentence again and note the explicit reason of
capturing content.

Explaining what the real task actually is can lead to simpler/unconsidered
solutions.

Hopefully this solution is suitable for your needs.

Nope

I've tried and apparently failed to explain that a custom
ContentPlaceHolder is not required to accomplish the task. If you feel
it is necessary to use a ContentPlaceHolder-derived type to capture
the content then do it and deal with the designer issue. If the
designer issue shows itself to be an obstacle, you may want to
reconsider possible alternatives.

regards
A.G.


regards
A.G.

.



Relevant Pages

  • Re: GridView DataBind() Not Working
    ... code(CreateChildcontrols), the designer (of your toplevel custom control) ... it hasn't a designer associated like the top ... render the control manually. ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • Re: Custom ContentPlaceHolder
    ... Render method without infinite recursion? ... StringBuilder sb = new StringBuilder; ... It also worth noting that the custom ContentPlaceHolder control works just ... this a 100% designer issue. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Custom ContentPlaceHolder
    ... Render method without infinite recursion? ... It also worth noting that the custom ContentPlaceHolder control works just ... this a 100% designer issue. ... The string s will contain the ContentPlaceHolder's content. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: CreateChildControls(...) or Render(...)?
    ... You can simply associate your control to a custom control designer class ... method into another sub and have your createchildcontrols method call this. ... This way you can call this new sub from your designer class and render the ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • Web derived controls in VS2005 designer view
    ... I have a control derived from a drop down list written in VB.NET ... ....the drop down list will render in both the designer and IE. So I am ... listitems, but will render static listitems from dropdownlists. ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)

Loading