Databases Reference
In-Depth Information
Although the current implementation of the
Region
field template satisfies your immedi-
ate requirements, it also suffers from a serious problem. By accessing the
DataControl
of
the
Country
field template directly and casting it down to the
TextBox
type, the Region
field template assumes that the
Country
field template will always use a
TextBox
as its data
control. Although this assumption is correct today, it easily breaks as soon as you decide
to change what is really an internal implementation of how the
Country
property is
presented on the web page. You could decide to create a custom field template with a
DropDownList
so that users can pick a country instead of typing its name. You could also
decide to change the
Text_Edit
field template to use a third-party control instead of the
built-in
TextBox
. Any number of changes like this breaks the
Region_Edit
field template.
As the number of dynamic and custom field templates increases, improving encapsulation
helps to keep their maintenance simple. The need to access the current field value and
detect its changes is a common problem. You can solve it by introducing an extended base
class for field templates that provides a common way to access this information without
having to take a dependency on the specific implementation details of a particular
template. In the sample solution accompanying this topic, this new base class is called
UnleashedFieldTemplate
; you can see its complete source code in Listing 10.16.
LISTING 10.16
UnleashedFieldTemplate Base Class
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Unleashed.DynamicData
{
public class UnleashedFieldTemplate : FieldTemplateUserControl
{
private static readonly object FieldValueChangedEvent = new object();
public virtual bool AutoPostBack { get; set; }
public override object FieldValue
{
get { return base.FieldValue; }
set
{
if (base.FieldValue != value)
{
base.FieldValue = value;
this.OnFieldValueChanged(EventArgs.Empty);