Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > RE: Custom ContentPlaceHolder

Reply
Thread Tools

RE: Custom ContentPlaceHolder

 
 
Registered User
Guest
Posts: n/a
 
      06-26-2009

On Thu, 25 Jun 2009 16:40:20 -0400, "gerry" <(E-Mail Removed)>
wrote:
>
>"Registered User" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>> On Thu, 25 Jun 2009 13:12:37 -0400, "gerry" <(E-Mail Removed)>
>> 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?
>>
>> 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.

>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();

The string s will contain the ContentPlaceHolder's content. If the
content can be captured without using a derived class, does the valid
reason to derive a class still exist? Explaining what the real task
actually is can lead to simpler/unconsidered solutions. Hopefully this
solution is suitable for your needs.

regards
A.G.
 
Reply With Quote
 
 
 
 
gerry
Guest
Posts: n/a
 
      06-26-2009


"Registered User" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Thu, 25 Jun 2009 16:40:20 -0400, "gerry" <(E-Mail Removed)>
> wrote:
>>
>>"Registered User" <(E-Mail Removed)> wrote in message
>>news:(E-Mail Removed). ..
>>> On Thu, 25 Jun 2009 13:12:37 -0400, "gerry" <(E-Mail Removed)>
>>> 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
}

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

> The string s will contain the ContentPlaceHolder's content.


No it won't.

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

> Explaining what the real task actually is can lead to simpler/unconsidered
> solutions.
> Hopefully this solution is suitable for your needs.


Nope

>
> regards
> A.G.



 
Reply With Quote
 
 
 
 
Registered User
Guest
Posts: n/a
 
      06-26-2009

On Fri, 26 Jun 2009 10:40:32 -0400, "gerry" <(E-Mail Removed)>
wrote:

>
>"Registered User" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>> On Thu, 25 Jun 2009 16:40:20 -0400, "gerry" <(E-Mail Removed)>
>> wrote:
>>>
>>>"Registered User" <(E-Mail Removed)> wrote in message
>>>news:(E-Mail Removed) ...
>>>> On Thu, 25 Jun 2009 13:12:37 -0400, "gerry" <(E-Mail Removed)>
>>>> 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.

>

 
Reply With Quote
 
Registered User
Guest
Posts: n/a
 
      06-27-2009

On Fri, 26 Jun 2009 16:41:34 -0400, "gerry" <(E-Mail Removed)>
wrote:

>attached is a simple project with a master page containing a single
>ContentPlaceHolder
>and a ContentPage that places a single textbox in the ContentPlaceHolder
>
>trying to capture the generated ContentPlaceHolder content in the
>ContentPlaceHolder1_PreRender , Page_Load or Page_PreRenderComplete
>all generate the same "must be placed inside a form tag with runat=server. "
>error.
>
>I must be doing something wrong if you can do this without any errors - any
>idea what ?
>

You used the wrong server control. No, seriously I used a label in my
quickie test.
-snip -
>>>

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

>
>And I was asking in case you see something that I don't.
>

-snip -
>> Yes and the OnPreRender event is only fired once.

>
>Typo there - should have been Render will be called again.

No problem with it being a typo. These things happen.
>
>>

- snip -
>> http://www.4guysfromrolla.com/articles/091102-1.aspx

>
>and that article also links to a second article because it only works with
>pre 2.0 versions of the framework.
>the method generates errors in 2.0+ - he did run into a different error so
>there are obviously multiple problems with this solution
>calling the Render method at any time before the Page_PreRenderComplete has
>fired could generate invalid results due to code in the contained controls
>PreRender that will not have had a chance to fire.
>

I scanned the article quickly as I've got my own managers trying to
boil the ocean.
- snip -
>>
>>>> Hopefully this solution is suitable for your needs.
>>>
>>>Nope
>>>

And now I see why it's not suitable and understand your frustration
with a derived ContentPlaceHolder type's behavior.

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

>

I should have written "may not be required".

>We are pretty much giving up on this for now.
>

I try to avoid asking the 'why' question because too often the
question is taken as a personal affront. The question isn't really
about an 'requirement' being good or bad as much as 'is it really a
necessary requirement?' That question would be in my mind even if a
derived ContentPlaceHolder worked as it should in the designer.

Sometimes not doing something can be a viable alternative. This might
be suitable for you situation or maybe nested master pages. Another
option might be to put each ContentPlaceHolder inside of a div,
capture the entire page's content, and then parse the content for the
divs using a DOM. I'm not saying this would work but you might want to
try it and see depending upon the status of the requirement.

I appreciate the time you've taken to explain the issues you've run up
against. Hopefully that time is billable

regards
A.G.


 
Reply With Quote
 
gerry
Guest
Posts: n/a
 
      06-27-2009


"Registered User" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> On Fri, 26 Jun 2009 16:41:34 -0400, "gerry" <(E-Mail Removed)>
> wrote:
>

....

>
>>We are pretty much giving up on this for now.
>>

> I try to avoid asking the 'why' question because too often the
> question is taken as a personal affront. The question isn't really
> about an 'requirement' being good or bad as much as 'is it really a
> necessary requirement?' That question would be in my mind even if a
> derived ContentPlaceHolder worked as it should in the designer.
>
> Sometimes not doing something can be a viable alternative. This might
> be suitable for you situation or maybe nested master pages. Another
> option might be to put each ContentPlaceHolder inside of a div,


Actually we did get this to work by wrapping the ContentPlaceHolder in the
master page within a custom Panel. It requires an additional bit of markup
per placeholder but it lets us encapsulate the required functionality within
one control and it works.

> capture the entire page's content, and then parse the content for the
> divs using a DOM. I'm not saying this would work but you might want to
> try it and see depending upon the status of the requirement.
>
> I appreciate the time you've taken to explain the issues you've run up
> against. Hopefully that time is billable
>
> regards
> A.G.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MasterPage - ContentPlaceHolder - How to navigate to the ContentPlaceHolder "Child Page" Greg ASP .Net 4 04-17-2006 03:48 PM
MasterPage - ContentPlaceHolder - How to navigate to the ContentPlaceHolder "Child Page" Greg ASP .Net Web Controls 1 03-23-2006 10:17 AM
Can a <asp:contentplaceholder> generate custom events? Beatniks ASP .Net 0 12-23-2005 06:07 PM
[asp.net2] Masterpage und ContentPlaceHolder ausrichten Sebastian Meier ASP .Net 0 04-23-2005 09:06 PM
master pages contentplaceholder Billy Cormic ASP .Net 3 09-01-2004 03:13 PM



Advertisments