Online Book Reader

Home Category

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

By Root 5399 0
class MyDataContainer

{

public float Numbers { get; set; }

public String Label { get; set; }

}

protected void Page_Load(object sender, EventArgs e)

{

// Uses a random number as the value of the GaugeBar.

// The value is stored in a custom object.

Random rnd = new Random();

var container = new MyDataContainer();

container.Numbers = rnd.Next(0,100);

container.Label = "{0} out of {1}";

// Binds the DataTable to the GaugeBar

GaugeBar1.DataValueField = "Numbers";

GaugeBar1.DataTextField = "Label";

GaugeBar1.DataSource = container;

GaugeBar1.DataBind();

}

The DataTable has two columns—Numbers and Label—of type float and string, respectively. The table contains one data row. If the table contained multiple rows, only the first would be taken into account according to the code in PerformDataBinding.

Note that you can also use the DataItem property to bind data to the GaugeBar control:

GaugeBar1.DataItem.Value = 12;

GaugeBar1.DataItem.Text = "{0} %";

Note that no call to DataBind is required to trigger the process and update the control’s user interface.

Building a Composite Templated Control


The CompositeDataBoundControl class is the starting point for building rich, complex, and data-bound composite controls. A composite data-bound control must do the following:

Act as a naming container.

Create its own user interface through the CreateChildControls method.

Implement the necessary logic to restore its hierarchy of child elements after postback.

The good news is that you can completely ignore the third point if you derive your control class from the CompositeDataBoundControl class. The class, in fact, implements internally any necessary logic.

Generalities of Composite Data-Bound Controls


The main aspect you care about when building a composite data-bound control is designing the internal hierarchy of your control. The method to override for this purpose is an overloaded version of CreateChildControls. In addition, you typically add styles and templates.

In a real-world composite control, the internal control tree is usually quite complex. The outermost container is often a multirow HTML table (or perhaps a collection of

tags, each with specific semantics associated with it). However, what’s in the various cells and rows can vary quite a bit and result in a pretty sophisticated combination of child controls and literals.

Creating a Hierarchy of Child Controls


You should know by now that composite controls build their own interface by composing controls in the override of the CreateChildControls method. Defined on the Control class, the method has the following prototype:

protected override void CreateChildControls()

In the CompositeDataBoundControl class, the method is overridden and overloaded. In particular, the overridden version accomplishes a few interesting tasks. Here’s its pseudo-code:

protected override void CreateChildControls()

{

Controls.Clear();

var o = ViewState["_!ItemCount"];

if ((o == null) && RequiresDataBinding)

EnsureDataBound();

else

{

int numOfItems = (int) o;

object[] items = new object[numOfItems];

CreateChildControls(items, false);

base.ClearChildViewState();

}

}

The method first empties the Controls collection so that no pending child controls are left around. Next, it retrieves a value from a particular (and internally managed) view-state entry named _!ItemCount. The view-state entry caches the number of items that form the composite control. The code that actually builds the control tree is responsible for storing this value in the view state.

Knowing the number of items that form the control hierarchy is important to optimize the data-binding process. In ASP.NET, complex controls that show a possibly long list of data items are implemented as composite data-bound controls. In what way is this different from list and simple-bound controls?

List controls and simple-bound controls, such as the GaugeBar we considered earlier, cache the data item or items in the view state. In addition, they can either receive

®Online Book Reader