Programming Microsoft ASP.NET 4 - Dino Esposito [283]
The bottom line is that the amount of extra data that flows in the view state for a composite control is limited to the number of constituent items, and the control refreshes correctly after a postback. (Of course, child controls put in the view state the usual amount of data.)
The Control Item
It should be clear from the previous discussion that the ASP.NET team had excellent arguments to dictate that composite data-bound controls get their data exclusively from the data-binding process. This fact eliminates the need of having a kind of Items property on composite data-bound controls that works like the Items property of list controls. This said, feel free to add support for data item objects and collections to your composite controls if you need to.
Most composite controls feature a collection of items, but not a collection of data items. Each item represents a control item—that is, a logical building block of the control’s user interface. For a GridView, it is a GridViewRow object that represents a table row. For a sample BarChart control that displays a bar chart, the control item will be a class derived from TableRow that contains all the information needed to handle a single bar. The number of items that composite controls store in the view state is exactly the number of “control” items.
Let’s see how these concepts apply to a sample composite data-bound control such as BarChart.
The BarChart Control
The BarChart control inherits from CompositeDataBoundControl and defines the properties listed in Table 12-3.
Table 12-3. BarChart Properties
Property
Description
DataTextField
Name of the data field to use as the label of each bar.
DataTextFormatString
Format string for the display text.
DataValueField
Name of the data field to use as the value of each bar.
DataValueFormatString
Format string for the value to display on top of each bar.
Items
Collection of BarChart items. Each element represents a bar in the chart. Elements in the Items collection are of type BarChartItem.
Maximum
Gets and sets the maximum value that can be represented in the chart.
SubTitle
Gets and sets the subtitle of the final chart.
Title
Gets and sets the title of the bar chart.
The final markup for the control is a horizontal bar chart such as the one illustrated in Figure 12-4.
Figure 12-4. The BarChart control in action.
Each bar is fully represented by an element in the Items collection. In addition, the BarChart control features a few style properties, as Table 12-4 details.
Table 12-4. BarChart Style Properties
Property
Description
BarStyle
The style of the whole row that contains the bar
LabelStyle
The style of the label
SubTitleStyle
The style of the subtitle in the control’s header
TitleStyle
The style of the title in the control’s header
ValueStyle
The style of the element displaying the value rendered
The attributes of all style properties are applied in the Render method, as in other data-bound controls.
The BarChart Item Object
The user interface of the BarChart control is created in the overloaded version of CreateChildControls.
protected override int CreateChildControls(
IEnumerable dataSource, bool dataBinding)
{
return CreateControlHierarchy(dataSource, dataBinding);
}
Both input arguments are passed down to an internal CreateControlHierarchy method, which is ultimately responsible for the creation of the bar chart:
int CreateControlHierarchy(IEnumerable dataSource, bool dataBinding)
{
// Get the data to display (either from data source or viewstate)
if (dataSource == null)
{
RenderEmptyControl();
return 0;
}
//