ASP.NET
  Home arrow ASP.NET arrow Page 5 - Introducing ASP.NET 2.0: GridView Control
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

Introducing ASP.NET 2.0: GridView Control
By: Anand Narayanaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 64
    2005-07-11

    Table of Contents:
  • Introducing ASP.NET 2.0: GridView Control
  • Create the Database
  • Save and Run
  • Adding Paging Functionality to GridView
  • Updating 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


    Introducing ASP.NET 2.0: GridView Control - Updating Data


    (Page 5 of 5 )

     

    With ASP.NET 2.0, you can easily update the data directly using the GridView control by following the steps given below.

    1. Set the AutoGenerateEditButton property of GridView control to True. You will immediately find a link titled Edit inside the GridView control on the Design Mode.

    2. Set the UpdateQuery property of AccessDataSource Control appropriately. For this purpose, click on the ellipse and copy the following statement into the UPDATE Command text box.

      Listing 2

      UPDATE books SET Name = @Name, Price = @Price, Publisher =@Publisher WHERE Name = @Name

    3. Save and run the project. Click on the Edit link and your screen will look like the figure shown below. ASP.NET will automatically add the Update and Cancel link.

     

    Figure 11

    That's it. You're done. You are now ready to edit data. You will now feel how easy it is to edit the data using ASP.NET 2.0. 

    Deleting Data

    Deleting data is similar to editing, but it requires certain additional steps. First, you must set the AutoGenerateDeleteButton property to True. You will immediately notice a link titled Delete on the left side of the GridView control. You will then have to set the DeleteQuery property of the AccessDatSource Control. For this purpose, click on the ellipse from the properties window and copy the following statement into the DELETE command text box.

    Listing 3

    DELETE from books where Name = @Name

    Save and run the project. You can immediately delete the data by clicking the link against the appropriate row.

    Be careful while deleting data as it cannot be recovered once it is deleted. In order to avoid accidental deletion, it would be better to show a message box to the end user so that they can cancel the action if they don’t want to delete the item from the database.

    The code for displaying a message box is shown below. Switch to the Source view and enter the following code just below the </asp:BoundField> tag.

    Listing 4

    <asp:TemplateField>

                    <ItemTemplate>

                    <asp:LinkButton ID = "linkbutton1" runat = "server" CausesValidation ="false" CommandName = "Delete"

                    text = "Delete" OnClientClick = "return confirm('Pressing OK will delete this record. Do you want to continue')">

                    </asp:LinkButton>

                    </ItemTemplate>

    </asp:TemplateField>

    You will notice that a new Delete button was created on the right side of the Grid, as shown in Figure 12.

    Figure 12

    Run the project and click on the Delete button. You will see a message box as shown below.

    Figure 13

    If you select OK from the above displayed message box, the data against the selected row gets deleted.

    If you implement the above code, you can set the AutoGenerateDeleteButton property to False instead of True. Otherwise, you will find two Delete links inside the Grid.

     

    Summary

    With the evolution of ASP.NET 2.0, developers need not spend a long time before their computers and Microsoft have greatly reduced the development time by including more powerful components, which require much less code than what is required in ASP.NET 1.1. The GridView control, as discussed above, is one such example; there are many more controls available with ASP.NET 2.0. I will examine some of the interesting controls in future articles. In the meantime, I would suggest you take a look at the MSDN documentation for more information regarding all of the new controls included with ASP.NET 2.0.  


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · I'm Aannd - the author of this article. Please go through the article and let me...
       · I can't get this to work. I keep getting the error...
       · Hi, i am rachel.i did the method as u posted here. but my delete button doesnt...
       · Did your @<column name> match what was in the "<asp:BoundField DataField="?
       · hi I am algo getting the same errror with delete action.sudha
       · hello, how about when xml is our datasource?
     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT