Programming Microsoft ASP.NET 4 - Dino Esposito [266]
void SetPageProperties(int startRowIndex, int maximumRows, bool databind)
{
if ((this._startRowIndex != startRowIndex) || (this._maximumRows != maximumRows))
{
PagePropertiesChangingEventArgs e;
e = new PagePropertiesChangingEventArgs(startRowIndex, maximumRows);
if (databind)
{
this.OnPagePropertiesChanging(e);
}
this._startRowIndex = e.StartRowIndex;
this._maximumRows = e.MaximumRows;
if (databind)
{
this.OnPagePropertiesChanged(EventArgs.Empty);
}
}
if (databind)
{
base.RequiresDataBinding = true;
}
}
PagePropertiesChanging and PagePropertiesChanged events are fired before and after, respectively, each paging operation.
The data pager control is normally placed outside the ListView’s layout. In this case, you use the PagedControlID property of the data pager to specify the paged control. However, if the PagedControlID property is not specified, the data pager assumes that its naming container is the paged control (as long as it implements the IPageableItemContainer interface). What does this mean to you? It means you can embed the data pager in the layout template of the ListView control and avoid setting the PagedControlID property on the pager explicitly.
Sorting the List
The data bound to the ListView control can be sorted using a button in the layout template with the command name of Sort:
CommandArgument="companyname" />
You specify the sort expression and the initial sort direction using the CommandArgument property of the button. You use asc and desc to indicate the desired direction. Multiple sorting fields can be listed as well. The sorting automatically reverses from ascending to descending and vice versa as you click. The ListView’s SortExpression and SortDirection read-only properties tell you at any time about the current status of the sort.
Summary
The ListView control adds the benefits of ASP.NET view controls (such as the GridView or DetailsView control) to classic repeater data-bound controls such as DataList. The resulting control weds the extreme layout flexibility of a DataList or Repeater control with the power of two-way data binding of data source controls.
The ListView control can be used to implement virtually any reporting and publishing scenarios you can imagine. The distinct layout template gives you total control over the HTML being generated and the style it must have. Various item templates (regular, alternate, edit, selected, insert) let you decide about the markup to output for each possible state of the control.
Finally, the ListView control is a pageable control. Unlike other view controls, though, the ListView control binds to an external pager control—the new DataPager control. The connection between the two controls is all in the IPageableItemContainer interface. As a result, each data-bound control with this interface can be paged without incorporating the logic to page.
Chapter 12. Custom Controls
Ignorance, the root and the stem of every evil.
—Plato
Server controls are one of the pillars of the entire ASP.NET Web Forms framework. Server controls are compiled classes that encapsulate user-interface and other functionality into reusable packages. ASP.NET provides a full bag of stock controls to serve most developers’ needs. However, writing custom controls is possible and sometimes necessary. Custom controls are no different than standard ASP.NET server controls except that they are bound to a different tag prefix and must be registered and deployed explicitly. Aside from that, custom controls can have their own object model, fire events, and support all the design-time features of Microsoft Visual Studio, such as the Properties window, the visual designer, property builders, and the