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

Custom ASP.NET Controls


Choosing the Right Version of ASP.NET

If the suite of built-in ASP.NET server controls does not have a control with the characteristics you need, you can roll your own by deriving from Control or WebControl. In this way, you are responsible for implementing the programming interface and behavioral logic of the new control. You can even modify existing web controls by deriving classes of your own from them. In this case, the new control already has a well-defined behavior, and your task is to customize it to your desired extent.

Overall, it is worth noting that ASP.NET controls are limited in their functionality by the HTML language and HTML object model of the browser. When you need a certain functionality that no existing control provides, the first thing you should ask is whether that functionality can really be obtained from an HTML environment altogether and what support it expects from the browser. ASP.NET is primarily an abstraction layer built on top of HTTP and HTML, so its controls can’t reach functions that can’t be expressed with HTML elements and script code.

A control is made up of two key blocks of code—the programming interface and the rendering logic. The programming interface is the set of properties, methods, and events that let users and visual designer tools (i.e., Visual Studio .NET) interact with the control. dotnetfx.exe /C:"install /noaspupgrade"

By inheriting a base class—be it Control, WebControl, or another more specific control—you bring into the new class a well-defined set of members. You can add new ones or override some of them. As ab alternative to overriding a member, you can choose to replace the member of the base class with a new one having the same name and type. This normally happens when you want to restrict or enlarge the visibility of the member. For example, consider exposing a read/write property as read-only. To shadow a base property in C#, you use the “new” keyword instead of “override.” In Visual Basic .NET, you use “Shadows” in lieu of “Overrides.”

The rendering logic is the code that provides the HTML markup to be sent to the browser. You normally need to write this code from the ground up only if you’re building a control from either Control or WebControl. This need is significantly less frequent if you inherit your component from an existing, non-base control. For web controls, the entry point in the rendering process is the RenderControl method. The method is declared public in the Control class and inherited by all ASP.NET controls. The method outputs the content of the control to a provided HtmlTextWriter object and, if tracing is enabled, stores tracing information about the control.

The ASP.NET rendering mechanism, though, is extremely layered and involves several methods at various levels. The base behavior of the RenderControl method entails that a call is made into a protected, overridable method named Render. In the base implementation that provides the Control class, the Render method calls into another protected member named RenderChildren. As the name suggests, RenderChildren loops over the contents of the control’s Controls collection and polymorphically calls the RenderControl method on any child controls.

The WebControl class, which is a first-level child of Control, overrides RenderControl and implements it in three steps that recalls the structure of the final HTML output—begin tag, contents, and end tag. As a result, if you want to modify the output of a control, you should look at RenderContents and override it. Unlike RenderContents, which is protected, RenderBeginTag and RenderEndTag are declared public. They render the opening and the ending tag of the HTML text for the control, and as such they are much less subject to overriding than RenderContents.

When it comes to writing a new control from scratch, should you inherit from Control or WebControl? WebControl in turn inherits from Control and extends it with a fixed number of members related to the user interface (i.e., font and color properties). In many cases, WebControl is just fine, but look into its base members before you choose.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].


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.