Most web applications perform some sort of data access. The data access tools made available in
ASP.NET 1.x were far more powerful then anything available in classic ASP, but with that power came an
additional level of complexity. With ASP.NET 2.0, Microsoft has taken things to the next level yet again
by simplifying things without taking away the power we've become accustomed to.
In this article I'll introduce the new GridView control and show just how easy it is to build a basic
data display page.
Data Source Controls
ASP.NET 2.0 introduces the concept of a data source control. A data source control
represents whatever data source you'll be connecting to. As such it doesn't display anything
to the user, but simply waits for you to connect a data-bound control to it.
There are five data source controls: AccessDataSource, ObjectDataSource, SiteMapDataSource,
SqlDataSource, and XmlDataSource. The names are pretty much self-explanatory and each of them
allows you to connect to the corresponding data repository.
For the purposes of this article I'm going to be using the SqlDataSource since I'll be using
SQL Server's "pubs" sample database since it ships with SQL Server and saves me from
having to create and include a database in the zip file. Below is the data source control that
I'll be using in my example:
<asp:SqlDataSource ID="mySqlDataSource" runat="server"
ConnectionString="Server=(local);Database=pubs;User Id=sa;Password=password;"
SelectCommand="SELECT * FROM authors"
/>
As you can see there's really not much to it. You add the control to your page, give it a name,
set your connection string, and give it a SQL query that it uses to retrieve the data. That's it.
Now it's just sitting there waiting for you to use it.
The GridView Control
Now that we have our data source control in place we need some way to display the data from whatever
data store it connects to. Remember that I said the data source control doesn't display anything?
That means that if we attempt to view our web page in a browser as it stands, we'll just see a blank
web page. In order to display the data, I'm going to use the new GridView control. The GridView
is one of the new ASP.NET 2.0 data-bound controls. The best way to think of it is as a
new and improved DataGrid.
You use the GridView much as you'd use any other ASP.NET control. First you add it to your page and give it a name.
Then all you need to do is tell it where it should get its data from. In this case it'll be using the
SQL data source control we set up previously -- mySqlDataSource.
And here's what you get when you request it via a browser.
I know it's not the prettiest thing in the world, but who ever thought that writing a dynamic data-driven web
page could be so easy? Oh and it's not hard to make it pretty either. It's just not my thing and I'm trying to
keep things as simple as I can so that you can see just how easy this can be. That being said, the best part of all
is that it doesn't really even get hard when you try and do more complicated things. Let's take a look.
Paging and Sorting
I said earlier that the GridView could be thought of as a new and improved DataGrid. So what makes it
new and improved? Well plenty, but the things that are of most interest to us as a developers is the fact that
the GridView can take advantage of the capabilities of our data source. Instead of our needing to write code to
enable features like paging and sorting you can simply "flip a switch" and they are pretty much handled for you.
If you look at the listing below you'll see that all I've done is added the two lines in red.
<%@ Page Language="VB" %>
<html>
<head>
<title>Introducing the ASP.NET 2.0 GridView</title>
</head>
<body>
<form runat="server">
<asp:SqlDataSource ID="mySqlDataSource" runat="server"
ConnectionString="Server=(local);Database=pubs;User Id=sa;Password=password;"
SelectCommand="SELECT * FROM authors"
/>
Notice the column headings are now hyperlinks that when clicked will sort the table by that column.
Likewise the table now is displaying only some of the results and has links to the other pages at the bottom.
See how all we had to do was tell the GridView that we wanted to allow paging and sorting and it took
care of everything for us. No special code to write or events that we have to handle... it just works.
Making it Pretty
Earlier I mentioned how the display wasn't the prettiest thing you've probably seen on the web.
While I'm not going to do anything too fancy, I do want to take a second and show you that it
doesn't really have to be as bare bones looking as I've made it look.
Here's the same page with proper titles and some very basic formatting applied.
I'm not including the code listing for this page here, but it's in the zip file if you want to take a look.
That's All Folks
It's amazing just how much functionality you can get with so little effort when using the new data access
controls in ASP.NET 2.0. I've barely scratched the surface of all the cool things you can do, but
I think you'll find that even complex tasks are much simpler then they've ever been before.
I've focused on the GridView since the ASP.NET 1.x DataGrid was the most commonly used data control and
the GridView is the logical successor, but I think you'll that all the controls just as easy to work with.
So if your web apps do any sort of data access and you haven't already, it's probably time for you to jump on
the ASP.NET 2.0 bandwagon and start meeting your deadlines and maybe even take some of that vacation time
that's been building up.
Download the Code
You can download the code in zip file format from
here.
While the .NET Framework made building ASP.NET applications easier then it had ever been in the past, .NET 2.0 builds on that foundation in order to take things to the next level. This article shows you to how to construct an N-Tier ASP.NET 2.0 Web application by leveraging the new features of ASP.NET 2.0 and SQL Server 2005.
[Read This Article][Top]
With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.NET by introducing a suite of new features and functionalities. As part of this release, ASP.NET 2.0 also comes with a host of new special files and folders that are meant to be used to implement a specific functionality. This article examines these new files and folders in detail and provides examples that demonstrate how to utilize them to create ASP.NET 2.0 applications.
[Read This Article][Top]
Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
[Read This Article][Top]
Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server. [Read This Article][Top]
In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance. [Read This Article][Top]
In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance. [Read This Article][Top]
In ASP.NET 2.0 and Visual Studio 2005, you can quickly program custom authentication pages with the provided Membership Login controls. In this article, Dina Fleet Berry examines the steps involved in using the Login control with a custom SQL Server membership database.
[Read This Article][Top]
In this article, Thiru Thangarathinam examines .NET 2.0's new ClickOnce deployment technology that is designed to ease deployment of Windows forms applications. This new technology not only provides an easy application installation mechanism, it also eases deployment of upgrades to existing applications. [Read This Article][Top]
With ASP.NET 2.0, Microsoft has made great strides in increasing developer productivity and has made implementing previously complex solutions relatively easy. Where this version of ASP.NET really shines, however, is in its new administrative tools that allow developers to spend less time managing the configuration of the servers and software and more time developing great code.
[Read This Article][Top]
Thiru Thangarathinam introduces ASP.NET 2.0's new TreeView control which provides a seamless way to consume and display information from hierarchical data sources. The article discusses this new control in depth and explains how to use this feature rich control in your ASP.NET applications. [Read This Article][Top]
Mailing List
Want to receive email when the next article is published? Just Click Here to sign up.