Online Book Reader

Home Category

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

By Root 5603 0
Start building the hierarchy of controls

Table t = new Table();

Controls.Add(t);

// Add the header row with the caption

CreateTitle(t);

// Add the subtitle row

CreateSubTitle(t);

// Add bars

int totalItems = CreateAllItems(t, dataSource, dataBinding);

return totalItems;

}

The control hierarchy is a table with two rows for the title and subtitle and other rows for the bars of the chart. CreateAllItems adds bar chart items and counts their number. This number is then returned and ends up in the view state.

int CreateAllItems(Table t, IEnumerable data, bool useDataSource)

{

// Count how many items we add

int itemCount = 0;

// Clears the Items collection (creates it, if null)

Items.Clear();

// Scroll data items, and create table items

foreach (object o in data)

{

// Create the match item object

BarChartItemType itemType = BarChartItemType.Item;

BarChartItem item = CreateBarChartItem(t,

itemType, o, useDataSource);

// Add the newly created object to the Items collection

_items.Add(item);

// Increase the counter

itemCount++;

}

// Return how many items we have into the viewstate (for postbacks)

return itemCount;

}

For each bound item, the method creates a BarChartItem object and adds it to the Items collection. We’ll discuss the BarChartItem class in a moment.

Note that you use Items.Clear to clear the collection and _items.Add to add a new bar chart item to the collection. The Items property is implemented as follows:

private BarChartItemCollection _items;

...

public virtual BarChartItemCollection Items

{

get

{

if (_items == null)

_items = new BarChartItemCollection();

return _items;

}

}

The property Items uses the _items variable as its storage medium. The first call to Items.Clear ensures that the collection is properly initialized. The second call to the same collection can go through the local variable to save a call to the get accessor of the Items property.

The BarChartItem class represents a bar in the chart and is defined as follows:

public class BarChartItem : TableRow

{

private object _dataItem;

private BarChartItemType _itemType;

public BarChartItem(BarChartItemType itemType)

{

_itemType = itemType;

}

public object DataItem

{

get {return _dataItem;}

set {_dataItem = value;}

}

public BarChartItemType ItemType

{

get {return _itemType;}

}

}

The class inherits from TableRow (actually, a bar in the chart is a table row) and defines a couple of properties: DataItem and ItemType. The DataItem property references the data item in the bound data source associated with the corresponding item. For example, if the BarChart is bound to a DataTable, DataItem is bound to the DataRow that corresponds to a given bar. ItemType, on the other hand, indicates the type of table row—such as a title, subtitle, or item. The item types are defined through an enumerated type:

public enum BarChartItemType

{

Title,

SubTitle,

Item

}

The Items property groups a bunch of BarChartItem objects in a collection. The collection type is BarChartItemCollection:

public class BarChartItemCollection : Collection

{

}

Because bar chart item objects don’t go to the view state, there’s no need to implement IStateManager and add extra view-state management methods as we did previously for the hyperlink control.

Adding Bound Data


With a composite data-bound control, you don’t need to override the PerformDataBinding method. However, you should pay some attention to keeping neatly separated the code that builds the structure of the control and the code that adds data.

The CreateBarChartItem method creates a new table row and enriches it with a DataItem property. What’s the content of the row? Looking at Figure 12-3, you can see that each table row has a cell for the label and a cell for the progress bar.

BarChartItem CreateBarChartItem(Table t, BarChartItemType itemType,

object dataItem, bool useDataSource)

{

// Create a new row for the outermost table

var item = new BarChartItem(itemType);

// Create cells for label and value

Return Main Page Previous Page Next Page

®Online Book Reader