Online Book Reader

Home Category

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

By Root 5604 0
GroupItemCount property. Let’s consider the following layout and group templates:

It indicates that the final output will be an HTML table where a new row is created for each group of items. Each table row contains as many cells as the value of GroupItemCount sets. The default value is 1. Note that in the preceding code snippet we’re using the default names for group and item containers—that is, groupPlaceholder and itemPlaceholder. When these names are used, there’s no need to set corresponding GroupPlaceholderID and ItemPlaceholderID properties on the ListView markup. Here’s the top-level markup for a tiled layout:

DataSourceID="ObjectDataSource1" GroupItemCount="4">

...

As an example, if you set GroupItemCount to 4, you’ll have rows of 4 cells each until there are less than 4 records left. And after that? What if the number of bound records is not a perfect multiple of the group item count? That’s where the EmptyItemTemplate property fits in:

This template is used to complete the group when no more data items are available. Figure 11-6 shows a typical tiled output you obtain by employing a ListView control.

Figure 11-6. A four-cell tiled layout built with the ListView control.

Using the Group Separator Template


Each group of items can be separated by a custom block of markup defined through the GroupSeparatorTemplate property. Here’s an example:

 

If you add this markup to the preceding example, you’ll display a blank row in between rows with data-bound cells. It’s a kind of vertical spacing.

The same can be done horizontally to separate data-bound cells within the same table row. To do so, you use the ItemSeparatorTemplate property instead. In both cases, the markup you put in must be consistent with the overall markup being created for the whole ListView control.

Modifying the Group Item Count Dynamically


The GroupItemCount property is read-write, meaning that you can change the size of each group programmatically based on some user actions. The following code snippet shows a pair of event handlers associated with the Click event of two Button controls:

protected void Button1_Click(object sender, EventArgs e)

{

// There's no upper limit to the value of the property

ListView1.GroupItemCount += 1;

}

protected void Button2_Click(object sender, EventArgs e)

{

// The property can't be 2 or less

if (ListView1.GroupItemCount >2)

ListView1.GroupItemCount -= 1;

}

The GroupItemCount property itself can’t take any value less than 1, but it has no upper limit. However, it should not accept any value larger than the actual number of data items currently bound.

As you assign a new value, the set modifier of the property resets the internal data-binding flag and orders a new binding operation. If you change the value of GroupItemCount over a postback, the ListView control automatically renders the updated markup back to the client. (See Figure 11-7.)

Figure 11-7. Changing the size of ListView groups dynamically.

The ListView control doesn’t natively support more advanced capabilities—such as uneven groups of items where, for example, the association between an item and a group is based on a logical condition and not merely determined by an index. In this scenario, you could have a list where the first group contains customers whose name begins with A and the second group contains those beginning with B, and so on. You would have to provide the logic for this yourself. Let’s look at this next.

Data-Driven Group Templates


The support for groups built into the ListView control is not data driven. In other words, the layout (groups and items) is first created

Return Main Page Previous Page Next Page

®Online Book Reader