Online Book Reader

Home Category

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

By Root 5484 0
control provides the greatest flexibility as far as the generation of the markup is concerned. It is not limited to a single record like FormView and DetailsView are, and it is not limited to a tabular layout like the GridView is. It is essentially a repeater with rich layout capabilities (like a DataList control) and the two-way data-binding capabilities of other view controls.

Simple Data Binding


You use the ListView control to generate any user interface that needs to be built as you iterate a collection of records. You associate data with a ListView control using the DataSource property or, better yet, using the DataSourceID property. In the former case, you explicitly provide the data and control any aspect of the binding process. The DataSourceID property connects the control to a data source component. The binding process is mostly automatic, but it works both ways—it reads and saves data. The following data source control populates a ListView control with customers who reside in the United States:

TypeName="DAL.CustomerRepository"

SelectMethod="LoadByCountry"

OldValuesParameterFormatString="original_{0}">

The data source control invokes the LoadByCountry method on the specified business object and makes the response available to any bound control. Let’s use a ListView control:

DataSourceID="ObjectDataSource1"

ItemPlaceholderID="ListViewContent">

<%-- ListView contents display here --%>

,  


In this example, the ListView control comprises three templates: layout, item, and item separator. Of the three, only the item separator template is optional. The layout template defines the overall structure of the output. The

element in the layout marked with the runat=server attribute represents the insertion point for a pair of item and item separator templates. The item template is finally filled with the actual data from the n.th record. The Eval method evaluates the specified property on the data item being currently bound. The Eval method works in reading; as we’ll see later, the Bind method works also in writing.

The item markup is made of a company name and country separated by a comma, and they are vertically separated from one another by a horizontal rule. Figure 11-1 shows the final results.

Figure 11-1. A simple ListView control in action.

Defining the Layout of the List


Most templated ASP.NET controls provide optional header and footer templates along with a repeated and data-bound item template. Header and footer templates are instantiated only once each, at the beginning and end, respectively, of the data binding loop. You can hide the header and footer, but most controls implicitly force you to think about the layout in terms of three components placed vertically: the header, body, and footer.

In this regard, the ListView control is different. It has no header or footer template, and it features just one template for the structure of the resulting markup: the layout template. If you need a header or a footer, you can easily place them in the layout. But if you need to develop the layout horizontally or in a tiled manner, the ListView approach makes it easier.

Up until ASP.NET 3.5, the layout template was mandatory in any ListView control. This is no longer the case, however, with ASP.NET 4. You can use the layout template as follows:

...

Instead of the

®Online Book Reader