Introduction

This tip describes the working of DetailsView for inserting a new record, Updating and Deleting the existing details of the user.

Background

The idea behind using the DetailsView control for displaying, updating and deleting data is because DetailsView control gives the ability to display, edit, insert, or delete a single record at a time from its associated data source. By default, the DetailsView control displays each field of a record on its own line.

The DetailsView control is typically used for updating and inserting new records, often in a master/detail scenario where the selected record of the master control determines the record to display in the DetailsView control. The DetailsView control displays only a single data record at a time, even if its data source exposes multiple records. The DetailsView control does not support sorting but it supports paging.

Using the Code

To bind the DetailsView control to a data source:

In Design view, right-click the DetailsView control, and then click Show Common Control Tasks.

On the Common DropDownList Tasks menu, click an existing data source or <New Data Source...> in the Choose Data Source dropdown.

If you choose <New Data Source...>, configure a new data source in the Data Source Configuration Wizard.

Now when we have the datasource ready, we can use the OnDataBound event to show the data in the required template in the following manner:

Now we have seen how to show and insert the data, what we need now is to update the item, we need to handle the ItemUpdating event. In this event, we will have to take the new values given by the user and take the required action like putting/updating in database.

Point of Interest

In this tip, we have seen how to perform insert, update and delete operations using DetailsView control in ASP.NET. It is rather easy, but many new developers seem to be struggling with it so I wrote this tip to help them.

Share

About the Author

I am a Software Engineer from Bhopal. I started my Career from Programming in ASP and now working as a Web Developer in ASP.Net (C#). I Love coding and always ready to gain new thing and always been towards Microsoft Technologies. Apart from coding my other hobbies are traveling, Internet Surfing, spending time with family and hang out with friends.