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: 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. That's it. No really! Here's the complete listing to GridView.aspx (which is also in the accompanying zip file).
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.
And here's the resulting page:
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.
0 comments:
Post a Comment