Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Shrink ASP.NET Code by 70 Percent
ASP.NET 2.0's new features let you create and maintain data-driven Web apps with 70 percent less code than you needed with ASP.NET 1.0.
by Thiru Thangarathinam

Posted February 6, 2004

Technology Toolbox: ASP.NET

ASP.NET 2.0 can reduce the number of lines of code an ASP.NET application requires by a whopping 70 percent. ASP.NET 2.0's new data source controls and data-aware controls go a long way towards producing this result. I'll show you how to harness the new features in your own applications.

ASP.NET 1.0 lacked a declarative model for binding data to data-aware controls such as DataGrid, DataList, and Repeater. ASP.NET 2.0 fixes this with a declarative model for binding data directly from the database. The new data controls' advanced concepts include enabling caching on a data source control and declaratively passing values to the SQL statement parameters directly from a control value.

ADVERTISEMENT

You begin binding data to data-aware controls by creating a data source control that encapsulates common functionality such as reading, writing, deleting, and inserting from the database. Once you have the data in the data source control, you can bind the data to data-aware controls, such as a dropdown list or a checkbox.

ASP.NET 2.0 provides half a dozen data source controls. <asp:SqlDatasource> enables you to select, update, delete, and insert data using SQL commands. It works with SQL Server, OLE DB, ODBC, and Oracle databases. I'll show you an example of this control later on.

<asp:ObjectDatasource> allows you to integrate the ASP.NET presentation layer seamlessly with data returned from middle-layer objects. This helps you build clean separation and easier maintenance into your n-tier Web apps. Most Web apps employ an n-tier architecture, with middle-layer objects returning complex objects you have to process in your ASP.NET presentation. That's where this control comes in.

<asp:AccessDatasource> works with Access databases. <asp:XmlDatasource> allows you to bind to XML data, which can come from a variety of sources such as an external XML file or a DataSet object. Once the XML data is bound to the XmlDataSource control, this control can then act as a source of data for data-bound controls such as TreeView and Menu.

<asp:DataSetDatasource> lets you switch between XML and relational data views. This control also lets you specify an XSLT Transformation to restructure the XML data. <asp:SitemapDatasource> lets users navigate between pages in a Web site. You implement this control by first creating an XML file named app.sitemap that lays out the site's pages hierarchically. Having the site hierarchy in the app.sitemap file enables you to databind the SitemapDataSource control with the app.sitemap file. After that, you can bind the contents of the SitemapDataSource control to data-aware controls such as TreeView.

ASP.NET 2.0 complements these six data source controls with a pair of data-bound controls used to display data contained in the data source controls. <asp:gridview> displays the values of a data source in a table (as did ASP.NET 1.0's DataGrid control). Each column represents a field, and each row a record. You can bind a GridView control to a SqlDataSource control and to any data source that implements the System.Collections.IEnumerable interface. This control can also render data adaptively for different types of devices and browsers that make the request. You can use <asp:detailsview> in conjunction with the GridView control, and you can display the details of a specific record in the data source.




Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home