Various Data Bound Controls Used in ASP.Net

 Introduction

This article explains the data bound controls used in ASP.NET to display data in various forms and do various database activities such as Add, Edit, Update and Delete operations. The control makes the data more organized and presents the data in an efficient way for the viewers. This article explains each of them separately to explain their use.

Description

ASP.NET provides a wide variety of rich controls that can be bound to data. Under the Data tab of the Visual Studio Toolbox, you can get several controls under the Data tab that could be used to display data from a data source, like a database or XML file.

ToolBox

The standard ASP.NET data presentation controls are:

  • DataList
  • DetailsView
  • FormView
  • GridView
  • ListView
  • Repeater

We can divide these data presentation controls into the following two main groups. We will discuss each of them one by one.

table

First of all, we go to the first group that includes the four controls Repeater, DataList, GridView and ListView.

For demonstration purposes, we will use a table named StudentDetails in our sample database. Using this table, we will be able to display our data we stored in the table along with a different control. Let's now take a detailed look at all of the standard controls used in ASP.NET.

Repeater Control

The Repeater control was introduced with ASP.NET 1.0. The ASP.NET Repeater is a basic container control that allows you to create custom lists from any data available to the page. It provides a highly customized interface. It renders a read-only template; in other words, it supports only the ItemTemplate to define custom binding. The Repeater control is a Data Bind Control, also known as container controls. The Repeater control is used to display a repeated list of items that are bound to the control. This control may be bound to a database table, an XML file, or another list of items. It has no built-in layout or styles, so you must explicitly declare all layout, formatting and style tags within the controls templates. You would require writing an explicit code to do paging using this control. The Repeater repeats a layout of HTML you write, it has the least functionality of the rest of the three controls.

The Repeater control supports the following features:

  • List format
  • No default output
  • More control and complexity
  • Item as row
  • Paging, Sorting and Grouping requires custom code writing
  • only Web control that allows you to split markup tags across the templates
  • no built-in selection capabilities
  • no built-in support for edit, insert and delete capabilities
  • no built-in support for paging, sorting and grouping capabilities
  • no built-in layout or styles, need to declare all layout, formatting and style tags explicitly within the control's templates
  • Strictly emits the markup specified in its templates, nothing more and nothing less.

Using the data we stored in table Student of our sample database, the Repeater control will look like this.

student data

DataList Control

The DataList control was introduced with ASP.NET 1.0. DataList is the next step up from a Repeater; except you have very little control over the HTML that the control renders. DataList allows you to repeat columns horizontally or vertically. The DataList control renders data as a table and enables you to display data records in various layouts, such as ordering them in columns or rows. You can configure the DataList control to enable users to edit or delete a record in the table. We can use a DataList control where we need a single-column list. The DataList control works like the Repeater control, used to display the data in a repeating structure, such as a table. It displays data in a format that you can define using a template and styles. However, it arranges the data defined in the template within various HTML structures. This includes options for horizontal or vertical layout and it also allows you to set how the data should be repeated, as flow or table layout. The DataList control does not automatically use a data source control to edit data. Instead, it provides command events in which you can write your own code for these scenarios. You can configure the DataList control where the user can edit or delete a record in the table.

The DataList control supports the following features:

  • Support for binding data source controls such as SqlDataSource, LinqDataSource and ObjectDataSource
  • Directional rendering
  • Good for columns
  • Item as cell
  • Updatable
  • Control over Alternate item
  • Paging function needs handwriting.

After execution our ListView will look like this.

dataList

GridView Control

ASP.NET provides a number of tools for showing tabular data in a grid, including the GridView control. It was introduced with ASP.NET 2.0. The GridView control is used to display the values of a data source in a table. Each column represents a field where each row represents a record. It can also display empty data. The GridView control provides many built-in capabilities that allow the user to sort, update, delete, select and page through items in the control. The GridView control can be bound to a data source control, in order to bind a data source control, set the DataSourceID property of the GridView control to the ID value of the data source control. It's considered a replacement for the DataGrid control from .NET 1.1. Therefore, it is also known as a super DataGrid. The GridView control offers improvements such as the ability to define multiple primary key fields, improved user interface customization using bound fields and templates and a new model for handling or canceling events. Performance is slow compared to DataGrid and ListView.

The GridView control supports the following features:

  • Improved data source binding capabilities
  • Tabular rendering – displays data as a table
  • Item as row
  • Built-in sorting capability
  • Built-in select, edit and delete capabilities
  • Built-in paging capability
  • Built-in row selection capability
  • Multiple key fields
  • Programmatic access to the GridView object model to dynamically set properties, handle events and so on
  • Richer design-time capabilities
  • Control over Alternate item, Header, Footer, Colors, font, borders, and so on.
  • Slow performance as compared to Repeater and DataList control

gridview

ListView Control

The ListView control was introduced with ASP.NET 3.5. The ListView control resembles the GridView control. The only difference between them is that the ListView control displays data using user-defined templates instead of row fields. Creating your own templates gives you more flexibility in controlling how the data is displayed. It enables you to bind to data items that are returned from a data source and display them. The data can be displayed in pages where you can display items individually, or you can group them. The template contains the formatting, controls and binding expressions that are used to lay out the data. The ListView control is useful for data in any repeating structure, similar to the DataList and Repeater controls. It implicitly supports the ability to edit, insert and delete operations, as well as sorting and paging functionality. You can define individual templates for each of these scenarios.

Notice that, the ListView control is the only control that is implementing the IPageableItemContainer interface, so it can use a DataPager control.

The ListView control supports the following features:

  • Binding to data source controls Customizable appearance through user-defined templates and styles.
  • Built-in sorting and grouping capabilities
  • Built-in insert, edit and delete capabilities
  • Support for paging capabilities using a DataPager control.
  • Built-in item selection capabilities
  • Multiple key fields
  • Programmatic access to the ListView object model to dynamically set properties, handle events and so on
  • Fast performance as compared to GridView

ListView

Repeater vs. DataList vs. GridView vs. ListView

The Repeater, DataList, GridView and ListView controls have many common traits. Because of similarities between controls, it is usually not hard to switch to another control if the first choice is not good. The DataList control differs from the Repeater control in that the DataList control explicitly places items in an HTML table, whereas the Repeater control does not.

The common problem of using a GridView is a large ViewState that could cause slow page loads. Default GridView paging opens a complete data set in the server's memory. For large tables or for high traffic websites, this will overload the web server's resources. Even the DataPager control of a ListView still opens all the records in memory. Also, pages are opened using JavaScript. That means only the first page is indexed by search engines. The solution could be to create a custom pager, but this takes time to create, test and optimize code, as well as later maintenance of a separate project. The ListView control can exceed the capabilities of a Repeater or DataList control, but GridView still has the advantage of faster implementation and short markup code.

Now for the second group. Here is the description of the two controls DetailsView and FormView.

DetailsView control

The DetailsView control was introduced with ASP.NET 2.0. The DetailsView control uses a table-based layout where each field of the data record is displayed as a row in the control. Unlike the GridView control, the DetailsView control displays one row from a data source at a time by rendering an HTML table. The DetailsView supports both declarative and programmatic data binding. The DetailsView control is often used in master-detail scenarios where the selected record in a master control determines the record to display in the DetailsView control. It shows the details for the row in a separate space. We can customize the appearance of the DetailsView control using its style properties. Alternatively, we can also use Cascading Style Sheets (CSS) to provide styles to a DetailsView control. A DetailsView control appears as a form of recording and is provided by multiple records as well as insert, update and delete record functions.

The DetailsView control supports the following features:

  • Tabular rendering
  • Supports column layout, by default two columns at a time
  • Optional support for paging and navigation.
  • Built-in support for data grouping
  • Built-in support for edit, insert and delete capabilities

DetailsView

FormView control

The FormView was introduced with ASP.NET 2.0. The FormView control renders a single data item at a time from a data source, even if its data source exposes a multiple records data item from a data source. It allows for a more flexible layout when displaying a single record. The FormView control renders all fields of a single record in a single table row. In contrast, the FormView control does not specify a pre-defined layout for displaying a record. Instead, you create templates that contain controls to display individual fields from the record. The template contains the formatting, controls and binding expressions used to lay out the form. When using templates, we can place any control such as a dropdown list, checkbox and we can even place tables and rich controls like a GridView and so on. A FormView is a databound control used to insert, display, edit, update and delete data in ASP.NET that renders a single record at a time. A FormView control is similar to a DetailView in ASP.NET but the only difference is that a DetailsView has a built-in tabular rendering whereas a FormView requires a user-defined template to insert, display, edit, update and delete data.

The FormView control supports the following features:

  • Template driven
  • Supports column layout
  • Built-in support for paging and grouping
  • Built-in support for insert, edit and delete capabilities

FormView

DetailsView vs. FormView Control

The DetailsView and FormView controls enable us to display a single data item, in ohter words a single database record at a time. Both controls enable the display, edit, insert and deletion of data items such as database records but with the requirement of a single data item at a time. Each of these two controls render the user interface in its own unique way. Compared to the DetailsView control, the FormView control gives more flexibility over the rendering of fields. This form of rendering data enables more control over the layout of the fields. Using the FormView control is more complex as compared to the DetailsView control.

The major difference between these controls is that the DetailsView control displays a single database record as a table based layout. In this layout, data recorded for each field appears as a row in the control and the FormView control uses a template to display a single database record at a time. The template contents is used to set the form layout format, controls and a binding expression. Both the controls, DetailsView and FormView controls support page forward and backward traversing.

Note: The page forward and backward traversing allows us to move through the records one at a time both in the forward and backward direction.

Summary:

table Comparison

Yes: It means that it's provided by the control.
No: It means that it's not provided by the control but it could be possible to implement it explicitly.

Conclusion

This article provides a basic introduction to various controls of a Visual Studio Toolbox that could be used to display the data in various forms. I also covered similarities and differences among ASP.NET controls. I hope that this article has helped you in understanding the various controls available in ASP.NET. In future tutorials, we'll learn the implementation of these controls in ASP.NET programming.