Online Book Reader

Home Category

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

By Root 5276 0
provides its same set of basic capabilities, plus a long list of extensions and improvements. As mentioned, the DataGrid—which is still fully supported in ASP.NET—is an extremely powerful and versatile control. However, it has one big drawback: it requires you to write a lot of custom code, even to handle relatively simple and common operations such as paging, sorting, editing, or deleting data. The GridView control was designed to work around this limitation and make two-way data binding happen with as little code as possible. The control is tightly coupled to the family of new data source controls, and it can handle direct data source updates as long as the underlying data source object supports these capabilities.

This virtually codeless two-way data binding is by far the most notable feature of the new GridView control, but other enhancements are numerous. The GridView control is an improvement over the DataGrid control also because it has the ability to define multiple primary key fields, new column types, and style and templating options. The GridView also has an extended eventing model that allows you to handle or cancel events such as inserting, deleting, updating, paging, and more.

The ListView Control


The ListView control is fully template-based and allows you to control all aspects of the user interface via templates and properties. ListView operates in a way that closely resembles the behavior of existing data-bound controls, such as FormView or DataList. However, unlike these controls, the ListView control never creates any user-interface layout on its own. Every markup tag that the control emits is entirely under the developer’s control, including header, footer, body, item, selected item, and so on.

The ListView control binds to any data source control and executes its set of data operations. It can page, update, insert, and delete data items in the underlying data source as long as the data source supports these operations. In most cases, no code is required to set up any of these operations. If code is required, you can also explicitly bind data to the control using the more traditional DataSource property and related DataBind method.

The rendering capabilities of the ListView control make it suitable for publishing scenarios where a read-only, but compelling, user interface is needed. The control also works great in editing scenarios even though it lacks some advanced features such as input validation or made-to-measure edit templates for particular types of data or foreign keys.

I’ll say more on the ListView control in Chapter 11.

Data-Binding Expressions


As you might have figured out, most of the differences between the various data-bound controls is in how they use custom templates. A template is a piece of markup that the control injects in the page at a very specific point. More interestingly, the template contains bindable elements, which are placeholder markup elements whose content is determined by bound data.

How would you define the content of such bindable elements? In ASP.NET, a special syntax is required that we’ll examine right away. After this, we’ll return to the two most widely used view controls and examine some of their advanced capabilities.

Simple Data Binding


A data-binding expression is any executable code wrapped by <% … %> and prefixed by the symbol #. Typically, you use data-binding expressions to set the value of an attribute in the opening tag of a server control. A data-binding expression is programmatically managed via an instance of the DataBoundLiteralControl class.

Note

The binding expression is really any executable code that can be evaluated at run time. Its purpose is to generate data that the control can use to bind for display or editing. Typically, the code retrieves data from the data source, but there is no requirement that this be the case. Any executable code is acceptable as long as it returns data for binding. A data-binding expression is evaluated only when something happens that fires the control’s DataBinding event.

The following

Return Main Page Previous Page Next Page

®Online Book Reader