Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Taking Control of Page and Control Rendering


ASP.NET server controls are designed to accept property values from hosting pages and generate a chunk of markup to be incorporated in the HTTP response. As a page developer, though, you have no way to override such a hard-coded behavior. All that you can do — and it is not minor after all — is deriving a custom control and override the rendering engine to match your expectations. Deriving a custom control, though, may require a different skill set that not all Web page authors have.

Overall, deriving a new control from an existing one prefigures a relatively advanced approach to customization. You need to have a good reason to derive and use a custom control. There's some complexity involved with custom controls — deployment of a new assembly, maintenance of the related source code, testing.

What if you want to change the output of a built-in ASP.NET control on a page basis, that is without deriving a new control? Admittedly, this is not a task you face every day. However, it is still a question that guru developers can expect to be asked eventually. So what's the answer?

The System.Web.UI.Control class in ASP.NET supplies a method name SetRenderMethodDelegate which has the following prototype:


public void SetRenderMethodDelegate (RenderMethod renderMethod)

The method takes one argument representing the method that renders the specified control to the specified HTML text writer. The class RenderMethod is a delegate expressed as below:


public delegate void RenderMethod (
HtmlTextWriter output,
Control container
)

As the naming in the prototype may suggest, this approach won't work with just any control. Only container controls can take advantage of this hook technique. What are container controls? This is a hard question to answer. In general, a container control is just any control that happens to have children. In particular, as far as render delegates are concerned, being a container is a status that depends on the internal implementation of individual controls. For example, a Button and Label control is not a container per se; but what if you add child controls programmatically?


Label1.Controls.Add(new Control());
Button1.Controls.Add(new Control());

According to the previous definition, both Label1 and Button1 are containers. Nevertheless, only the Label control will successfully handle a render delegate. On the other hand, the SetRenderMethodDelegate method is considered an internal implementation detail and is not officially part of the public ASP.NET API open to developers. Still, the method is public, and you can use it — but at your own risk and provided that you know how to handle it.

The key fact is that ASP.NET controls are not consistent about how they support render delegates. The Control.Render method checks render delegates and invokes them if any. However, most ASP.NET controls override the Render method and some of them omit calling base.Render. For this reason, even though the support for rendering delegates is part of the framework, it is not supported by all controls. This is what happens with the Button control. The Label control, instead, does override the Render method. It doesn't call into the Control's implementation of the method, but still checks personally if any render delegate is registered. However, it does this only if child controls are found.

There's just one scenario in which the SetRenderMethodDelegate method is helpful — when you need to hook up the page rendering. The render delegate is used to handle ASP-style code blocks <% ... %> and in ASP.NET Ajax to implement page methods. If you're looking for a technique that lets you modify the output of a production page — without touching the source code — then render delegates are your best friends.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.