Bind Web Controls to a Custom Class
Well- designed applications rigorously separate their data
access logic from the rest of their code. In ASP.NET 2.0, you can
achieve this separation while still using the new ASP.NET data source
controls for convenient no-code-required design-time data binding. The
secret is to use the new ObjectDataSource control, which knows how to
fetch results from a data access class. You can then bind other controls
to the ObjectDataSource for quick and
easy web page display.
Note
Want to use data source binding without scattering database details throughout dozens of web pages? The ObjectDataSource control provides the solution.
How do I do that?
To use the ObjectDataSource
control, you must first create a custom class that retrieves the data
from the database. The database class will contain one method for
every database operation you want to perform. Methods that retrieve
results from the database can return DataTable or DataSet objects, collections, or custom
classes.
Example 4-1 shows a
database class called CustomerDB
that provides a single GetCustomers(
) method. The GetCustomers(
) method queries the database and returns a collection of
CustomerDetails objects. The
CustomerDetails object is also a
custom object. It simply wraps all the details of a customer record
from the database.
Example 4-1. A custom database class
Imports System.Data.SqlClient Imports System.Collections.Generic Public Class CustomerDB Private ConnectionString As String = _ "Data ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access