Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [268]

By Root 5311 0
of user-interface (UI) properties, such as style settings, colors, font, and borders. It inherits from Control.

Among the commonly used controls that inherit directly from Control, you find Repeater, MultiView, Placeholder, and LiteralControl. All other controls in ASP.NET inherit from one of these classes.

Extending a Base Class


The base Control class incorporates a number of features and makes them available to all child controls. A quick list includes view-state management, control identification, naming container capabilities, design-time support, themes, control state, and adaptive rendering. If you choose to inherit from any of the classes in Table 12-1, be prepared to write quite a bit of code because the control you get in the beginning is not particularly rich with concrete functionalities.

You typically inherit from any of those classes if you’re going to write a control that provides unique capabilities that are hard to find in other ASP.NET controls. Inheriting from any of the classes in Table 12-1 is more like building a custom control from scratch, where the effective starting point is determined by the selected base class.

If you opt for inheritance from a concrete control class—that is, a control that provides an observable behavior and user interface—you should strive to add new features or override existing capabilities without altering too much the structure and the personality of the control itself.

A Richer HyperLink Control


Let’s start with a sample custom control that extends the standard behavior of the HyperLink built-in control. By default, the ASP.NET HyperLink control outputs an anchor tag that points to a URL. By design, any click on an anchor tag is served directly by the browser, which navigates to the specified page. No postback occurs to the page that originally displayed the anchor. Put another way, if you want to track that the user clicked on a given anchor, you need to extend the behavior of the hyperlink control.

Designing a Usage Scenario


Let’s further develop the idea of a control that drives users to a different page but gives the page author a way to track the event. The canonical example used to illustrate the importance of this feature is the page hit counter. Monitoring the visitor activity is an important task that each administrator of a Web site should consider to improve the quality and content of the site. A click-through is the name commonly used to indicate the user’s clicking to see particular content, and it’s an important parameter for evaluating how the visitors of a site receive advertising. How would you implement a counter service that counts click-throughs in a page?

You can associate each button control in a page (Button, HyperLink, ImageButton, LinkButton, and AdRotator) with an extra layer of code that first tracks the click and then proceeds with the expected behavior. Getting this behavior with controls that entail a postback is not difficult. Take the LinkButton class, for example. You can derive a new control and override the OnClick protected member as follows:

protected virtual void OnClick(EventArgs e)

{

// Track the event

...

// Proceed with the default behavior

base.OnClick(e);

}

What about the HyperLink control, though? The click on the hyperlink is handled directly by the browser and doesn’t pass through any ASP.NET code of yours.

A Redirector for the HyperLink Control


The idea is that you force the HyperLink control to adopt a navigation URL that is different from the one set by the programmer. In other words, you divert the HyperLink to a custom page on your site where you first accomplish any custom tasks you need (such as tracking) and then redirect to the originally requested page. The code for such a modified version of the HyperLink control doesn’t look particularly scary:

using System;

using System.Web.UI.WebControls;

namespace Samples

{

public class Hyperlink : System.Web.UI.WebControls.HyperLink

{

public string RedirectPage

{

get

{

var o = ViewState["RedirectPage"];

if (o ==

Return Main Page Previous Page Next Page

®Online Book Reader