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
- Download my Sample.
- Change Connection String in web.config file.
- <connectionStrings>
- <add name="MyConnection"
- connectionString="Data Source=MONISH-PC\MONISH;Initial Catalog=master;Persist Security Info=True;User ID=saty;Password=1234"
- providerName="System.Data.SqlClient" />
- </connectionStrings>
- Execute the product table in SQL server.
- USE [master]
- GO
-
- /****** Object: Table [dbo].[Product] Script Date: 08/16/2012 19:46:27 ******/
- SET ANSI_NULLS ON
- GO
-
- SET QUOTED_IDENTIFIER ON
- GO
-
- SET ANSI_PADDING ON
- GO
-
- CREATE TABLE [dbo].[Product](
- [pk_id] [int] IDENTITY(1,1) NOT NULL,
- [ProductId] [varchar](5) NULL,
- [ProductName] [varchar](50) NULL,
- [ProductPrice] [numeric](8, 2) NULL,
- [ProductLocation] [varchar](25) NULL,
- [ProductQuantity] [varchar](10) NULL,
- PRIMARY KEY CLUSTERED
- (
- [pk_id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
-
- GO
-
- SET ANSI_PADDING OFF
- GO
- 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.
Add Record in DetailsView
Update Record Details View
Delete Record 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.