Programming Microsoft ASP.NET 4 - Dino Esposito [286]
{
_item = item;
}
// Properties
public BarChartItem Item
{
get { return _item; }
}
}
Both events are fired from within the CreateBarChartItem method:
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 the label and value
var labelCell = CreateLabelCell(item);
var valueCell = CreateValueCell(item);
var argsCreated = new BarChartItemEventArgs(item);
OnBarChartItemCreated(argsCreated);
...
if (useDataSource)
{
...
BarChartItemEventArgs argsData = new BarChartItemEventArgs(item);
OnBarChartItemDataBound(argsData);
}
}
Using the BarChart Control
Let’s see how to consume these events from within a host page. The following markup enables a BarChart control in an ASP.NET page:
OnBarChartDataBound="BarChart1_BarChartDataBound" > ...
Nothing in the preceding markup indicates the data source. In the Page_Load event, the control is bound to its data—a collection of custom objects with a couple of properties. One property indicates the amount of sales for an employee in the specified year; the other indicates the name of the employee:
protected void Button1_Click(object sender, EventArgs e)
{
var data = GetDataByYear(1997);
BarChart1.Maximum = 150000;
BarChart1.Title = "Northwind Sales";
BarChart1.SubTitle = "(Year 1997)";
BarChart1.DataSource = data;
BarChart1.DataTextField = "Employee";
BarChart1.DataValueField = "Sales";
BarChart1.DataBind();
}
The bar chart shown in Figure 12-3 is obtained by running the preceding code. The sample page handles the BarChartDataBound event through the following code:
void BarChart1_BarChartDataBound(object sender, BarChartItemEventArgs e)
{
// Get the amount of sales for the current bar
var sales = (Decimal) DataBinder.GetPropertyValue(
e.Item.DataItem, "sales");
// Add a ToolTip
var tip = sales.ToString();
e.Item.Attributes["title"] = tip;
// Highlight bar where sales > 50000
if (sales > 50000)
e.Item.Cells[1].BackColor = Color.LightGreen;
}
The amount of sales for the current employee is retrieved and added to the row as a ToolTip. In addition, if the sales are larger than 50,000, the cell is highlighted by using a different background color. (See Figure 12-5.)
Figure 12-5. Output of a BarChart control modified by page-level event handlers.
Note
All data-bound controls feature a couple of common events: DataBinding and DataBound. The former event fires before the data-binding process begins. The DataBound event, on the other hand, signals that the data-binding phase has terminated.
Adding Template Support
The BarChart control accepts two strings to display as the title and subtitle of the chart. Likewise, you can define a similar property for the footer. Title, subtitle, and footer are distinct items in the BarChart control hierarchy. What are you allowed to display in these items? As long as the properties are implemented as plain strings, there’s not much more than static text that can show up through the items.
A bit more flexibility can be added with format strings. A format string is a string that contains a predefined number of placeholders that the control machinery fills with internal data. For example, the FormatString property of the GaugeBar defaults to {0} / {1}—namely, a format string with two placeholders. The string is resolved as follows:
// First placeholder gets the Value to represent
// Second placeholder gets the Maximum value that can be represented
String.Format(FormatString, Value, Maximum);
You can enrich the format string with HTML tags to obtain more appealing results but, in the long run, this approach results in unmanageable code. A much better route to deep customizations of the user interface of controls is to use templates.
Templates and User Controls
In ASP.NET,