ASP.NET
  Home arrow ASP.NET arrow Page 2 - Retrieving Data with AJAX and the GridView...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Retrieving Data with AJAX and the GridView Control
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2008-08-28

    Table of Contents:
  • Retrieving Data with AJAX and the GridView Control
  • Auto-Generated Code
  • Adding Insert, Update, and Delete Statements
  • Displaying and Updating the Data

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Retrieving Data with AJAX and the GridView Control - Auto-Generated Code


    (Page 2 of 4 )

     

    Switch to Source view and look at the markup code that was generated for the GridView. It should appear as highlighted in Example 4-1.

    Example 4-1. GridView auto-generated control source code

    <%@ PAGE language="VB" autoeventwireup="true" codefile="Default.aspx.vb" inherits="_Default" %>

    <!DOCTYPE html public "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">
       
    <title>Untitled Page</title>
    </head>
    <body>
       
    <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <div>
                &nbsp;</div>
          
     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                       ConnectionString=
                         "<%$ ConnectionStrings:AdventureWorksConnectionString %>"
                         SelectCommand="SELECT [ProductID], [Name],
                         [ProductNumber],
                         [MakeFlag], [SafetyStockLevel], [ReorderPoint]
                         FROM [Production].[Product]" >
                   
    </asp:SqlDataSource>
                   
    <asp:GridView ID="GridView1" runat="server"
                      AllowPaging="True" AllowSorting="True" 
                 AutoGenerateColumns="False"
                   DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
                      <Columns>
                          <asp:BoundField DataField="ProductID" 
                     HeaderText="ProductID" InsertVisible="False"
                            ReadOnly="True"
              SortExpression="ProductID" />
                         
    <asp:BoundField DataField="Name" HeaderText="Name" 
                   SortExpression="Name" />
                         
    <asp:BoundField DataField="ProductNumber" 
                 HeaderText="ProductNumber"
          SortExpression="ProductNumber" />
                         
    <asp:CheckBoxField DataField="MakeFlag"  
                     HeaderText="MakeFlag"
               SortExpression="MakeFlag" />
                         
    <asp:BoundField DataField="SafetyStockLevel" 
               HeaderText="SafetyStockLevel"
        SortExpression="SafetyStockLevel" />
                         
    <asp:BoundField DataField="ReorderPoint" 
                  HeaderText="ReorderPoint"
           SortExpression="ReorderPoint" />
                      </Columns>
                </asp:GridView>
             </ContentTemplate>  
           </ASP:UPDATEPANEL> 
       
    </FORM>
    </BODY>
    </HTML>

    The IDE has done a lot of work for you. It has examined the data source and created a BoundField for each column in the data. Further, it has set the HeaderText to the name of the column in the database, represented by the DataField attribute. It has set the AllowPaging and AllowSorting properties to true. In addition, it has also set the SortExpression to the name of the field. Finally, you’ll notice on the declaration of the GridView that it has set AutoGenerateColumns to False.

    If you were creating the GridView by hand, and if you want to let the grid create all the columns directly from the retrieved data, you could simplify the code by setting AutoGenerateColumns to True. (If AutoGenerateColumns is set to true, and you also include explicitly bound columns, then you will display duplicate data.) To see this at work, create a second GridView by dragging another GridView control from the Toolbox inside the UpdatePanel, below the first.

    In the Smart Tag, set the Data Source to the same source as that of the first, SqlDataSource1. Click on the “Enable Paging” and “Enable Sorting” checkboxes.

    Now go to Source view. If necessary, delete the <columns> collection from the new grid, GridView2. Change AutoGenerateColumns to the default value: True. The declaration for this second GridView should look something like the following:

      <asp:GridView ID="GridView2" runat="server"
             
    AllowPaging="True" AllowSorting="True"
             
    DataSourceID="SqlDataSource1" >
      </asp:GridView>

    Run the page. Both grids behave identically and are visually indistinguishable. So why does the IDE create the more complex version? By turning off AutoGenerateColumns, the IDE gives you much greater control over the presentation of your data. For example, you can set the headings on the columns (such as changing ProductNumber to Product No.). You can change the order of the columns or remove columns you don’t need, and you can add new columns with controls for manipulating the rows.

    You can make these changes by manually coding the HTML in the Source view, or by switching to Design View and clicking the Smart Tag for the GridView and choosing Edit Columns. Do that now for GridView1 and you’ll see the Fields dialog box, as shown in Figure 4-14.

    This dialog box is divided into three main areas: the list of available fields, the list of selected fields (with buttons to remove fields or reorder the list), and the BoundField properties window on the right. When you click on a selected field (such as ProductID), you can set the way that field will be displayed in the data grid (such as changing the header to ID).

    While you’re examining what you can do with the GridView, let’s make it look a little nicer. First, delete or comment out the second (simpler) grid (GridView2) you just created a few moments ago. Second, open the Smart Tag on the original grid. Click AutoFormat and choose one of the formatting options. Of course, you can format it by hand, but why work so hard for a simple example? We’ll choose “Brown Sugar” because it shows up well in the printed book. Run the application. The output should appear as in Figure 4-15. 


    Figure 4-14.  The field editor dialog lets you change the properties of your data columns, without having to do it in Source view.

    More ASP.NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Learning ASP.NET 2.0 with AJAX: A...
     

    Buy this book now. This article is excerpted from chapter four of Learning ASP.NET 2.0 with AJAX: A Practical Hands-on Guide, written by Jesse Liberty, Dan Hurwitz and Brian MacDonald (O'Reilly, 2007; ISBN: 0596513976). Check it out today at your favorite bookstore. Buy this book now.

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek