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.
- 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.
- 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
- 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. |