Suppose
we have:
List<Car>
vCars = new List<Car>();
DataBoundControl
has a DataBind method that can be used when data is ready. It calls DataBind
for child controls as well. Page.DataBind() will call DataBind for all child
controls.
Using
DataSource Objects:
BaseDataBound control exposes DataSource property that accepts
objects that implement IEnumerable, IListSource, IDataSource, or IHierarchalDataSource.
DataSourceID accepts ID of SqlDataSource. If both specified
DataSourceID takes precedence.
GUI
based data sources DataSourceControl or
HierarchalDataSourceControl
implement IDataSource and IListSource interface.
|
GUI
Based Data Source
|
Description
|
|
AccessDataSource
|
MS Access (.mdb)
|
|
SqlDataSource
|
Open Database Connectivity(ODBC), Object
Linking and Embedding Databases(OLEDB), SQL Server, SQL (.mdf)
|
|
XmlDataSource
|
XML file within your project. Use XPath
for a subset, or XSL Transform can also be used.
|
|
ObjectDataSource
|
Any object implementing IEnumerable,
IListSource, IDataSource, IHierarchalDatasource can be used. Like a
collection, DataSet, DataTable can be used.
|
|
SitemapDataSource
|
A valid sitemap file at root of the
application.
|
Mapping
Fields to Templates:
Only
controls that support templates. Templates have no default interface. Controls
only provide binding support. Developer provides UI for templates. Templates
can also contain ASP.NET Binding commands. Controls that support templates are
GridView, DetailsView, and FormView.
|
Template
|
Description
|
|
HeaderTemplate
|
Optional. Header at the top of control.
|
|
FooterTemplate
|
Optional. Footer at the bottom of the
control.
|
|
ItemTemplate
|
ItemTemplate is rendered for each row of
the DataSource.
|
|
AlternatingItemTemplate
|
Optional. Just like ItemTemplate, But for
each odd index.
|
|
SelectedItemTemplate
|
Optional. Template for Seleted Item
Template.
|
|
SepratorTemplate
|
Optional. Template to seprate Item and
alternate item.
|
|
EditItemTemplate
|
Optional. Edit mode template.
|
Using the
DataBinder Class:
DataBinder
class provides a static method called Eval to access Data. Eval uses
reflection, looks for the item in DataItem and retrieves the value.
<#%
Eval(“VIN”) %>
An
overload also provides format capabilities.
<#%
Eval(“VIN”, “{0:C}”) %> //Currency
Eval
provides read only access.
ASP.Net 2.0 provides Bind that
allows updates as well.
<#% Bind(“VIN”) %>
<#% Bind(“VIN”, “{0:C}”) %>
GridView,
DetailsView, and FormView only support Bind. The control must have a
user-defines ID set, in order to user Bind.
Comments