asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Introducing the ASP.NET 2.0 GridView Control
By Ziran Sun
Rating: 2.8 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

  • download source code
  • 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.

      <asp:GridView ID="myGridView" DataSourceID="mySqlDataSource" runat="server" />

    That's it. No really! Here's the complete listing to GridView.aspx (which is also in the accompanying zip file).

    <%@ 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"
      />

      <asp:GridView ID="myGridView" DataSourceID="mySqlDataSource" runat="server" />

    </form>

    </body>
    </html>

    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"
      />

      <asp:GridView ID="myGridView" DataSourceID="mySqlDataSource" runat="server"
        AllowPaging  = "True"
        AllowSorting = "True"

      />
    </form>

    </body>
    </html>

    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.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jul 21, 2005 - N-Tier Web Applications using ASP.NET 2.0 and SQL Server 2005 - Part 1
    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]
    Apr 28, 2005 - New Files and Folders in ASP.NET 2.0
    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]
    Mar 10, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2, Cont'd
    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]
    Mar 9, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2
    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]
    Mar 3, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1, Cont'd
    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]
    Mar 2, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1
    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]
    Feb 16, 2005 - Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
    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]
    Dec 29, 2004 - ClickOnce Deployment in .NET Framework 2.0
    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]
    Dec 15, 2004 - A Sneak Peek at ASP.NET 2.0's Administrative Tools
    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]
    Nov 17, 2004 - The ASP.NET 2.0 TreeView Control
    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.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES