Every ASP
Every ASP
Every ASP.NET web form control inherits the DataBind method from its parent Control
class, which gives it an inherent capability to bind data to at least one of its properties. This is
known as simple data binding or inline data binding.
Simple data binding involves attaching any collection (item collection) which
implements the Enumerable interface, or the DataSet and DataTable classes to the DataSource
property of the control.
On the other hand, some controls can bind records, lists, or columns of data into their
structure through a DataSource control. These controls derive from the BaseDataBoundControl
class. This is called declarative data binding.
The data source controls help the data-bound controls implement functionalities such as,
sorting, paging, and editing data collections.
The BaseDataBoundControl is an abstract class, which is inherited by two more abstract
classes:
DataBoundControl
HierarchicalDataBoundControl
The abstract class DataBoundControl is again inherited by two more abstract classes:
ListControl
CompositeDataBoundControl
The controls capable of simple data binding are derived from the ListControl abstract class and
these controls are:
BulletedList
CheckBoxList
DropDownList
ListBox
RadioButtonList
The controls capable of declarative data binding (a more complex data binding) are derived
from the abstract class CompositeDataBoundControl. These controls are:
DetailsView
FormView
GridView
RecordList
Simple data binding involves the read-only selection lists. These controls can bind to an array
list or fields from a database. Selection lists takes two values from the database or the data
source; one value is displayed by the list and the other is considered as the value corresponding
to the display.
Let us take up a small example to understand the concept. Create a web site with a bulleted list
and a SqlDataSource control on it. Configure the data source control to retrieve two values from
your database (we use the same DotNetReferences table as in the previous chapter).
Choosing a data source for the bulleted list control involves:
When the application is executed, check that the entire title column is bound to the bulleted list
and displayed.
We have already used declarative data binding in the previous tutorial using GridView control.
The other composite data bound controls capable of displaying and manipulating data in a
tabular manner are the DetailsView, FormView, and RecordList control.
In the next tutorial, we will look into the technology for handling database, i.e, ADO.NET.
However, the data binding involves the following objects:
A dataset that stores the data retrieved from the database.
The data provider, which retrieves data from the database by using a command over a
connection.
The data adapter that issues the select statement stored in the command object; it is also
capable of update the data in a database by issuing Insert, Delete, and Update
statements.
Relation between the data binding objects:
Example
Let us take the following steps:
Step (1) : Create a new website. Add a class named booklist by right clicking on the solution
name in the Solution Explorer and choosing the item 'Class' from the 'Add Item' dialog box.
Name it as booklist.cs.
Step (2) : Add four list controls on the page a list box control, a radio button list, a check box
list, and a drop down list and four labels along with these list controls. The page should look
like this in design view:
Step (3) : Finally, write the following code behind routines of the application:
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.
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.
1. 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.
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.
2.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.
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.
3.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.
4. 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 layout 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 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
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 layout 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.
Template driven
Supports column layout
Built-in support for paging and grouping
Built-in support for insert, edit and delete capabilities
DetailsView vs. FormView Control
The DetailsView and FormView controls enable us to display a single data item, in other 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 renders 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.
Summary