Introduction

DetailsView is a DataBound control of ASP.NET used to display a single record from a DataSource in the form of a table, where each row represents a field of the record. The DetailsView control allows you to insert, edit and delete records.

Building the Sample

  1. Download my Sample.
  2. Change Connection String in web.config file.
    1. <connectionStrings>
    2. <add name="MyConnection"
    3. connectionString="Data Source=MONISH-PC\MONISH;Initial Catalog=master;Persist Security Info=True;User ID=saty;Password=1234"
    4. providerName="System.Data.SqlClient" />
    5. </connectionStrings>
  1. Execute the product table in SQL server.
    1. USE [master]
    2. GO
    3. /****** Object: Table [dbo].[Product] Script Date: 08/16/2012 19:46:27 ******/
    4. SET ANSI_NULLS ON
    5. GO
    6. SET QUOTED_IDENTIFIER ON
    7. GO
    8. SET ANSI_PADDING ON
    9. GO
    10. CREATE TABLE [dbo].[Product](
    11. [pk_id] [int] IDENTITY(1,1) NOT NULL,
    12. [ProductId] [varchar](5) NULL,
    13. [ProductName] [varchar](50) NULL,
    14. [ProductPrice] [numeric](8, 2) NULL,
    15. [ProductLocation] [varchar](25) NULL,
    16. [ProductQuantity] [varchar](10) NULL,
    17. PRIMARY KEY CLUSTERED
    18. (
    19. [pk_id] ASC
    20. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    21. ) ON [PRIMARY]
    22. GO
    23. SET ANSI_PADDING OFF
    24. GO
  1. Build the application and it will work fine.

Description

Initially, I have loaded data from a database to details view, each record is displayed in the details view of each page. After the user clicks the update button to edit and update selected record details, if he or she clicks the Add Row Button then they will be able to add a new record to the database.

AllowPaging >>> true/false

To check whether the DetailsView should support navigation.

ASP.NET

Add Record in DetailsView

Add Record in DetailsView

Update Record Details View

Record in DetailsView

Delete Record DetailsView

Record in DetailsView

Source Code Files

More Information

DetailsView Events I used in this sample:

  • PageIndexChanging
    This event fires when the DetailsView moves to another record. The first one fires before and the second one fires after a page is changed.

  • ItemUpdating
    This event fires when an item is updated. The first one fires before and the second one fires after the record is updated.

  • ItemDeleting
    This event fires when the current record is deleted. The first one fires before and other one fires after the record is deleted.