How to use DetailsView in asp.net

DetailsView Web Server Control
DetailsView is an asp.net web server control. DetailsView displays a single record from a data source. DetailsView provides a way to show, edit, update, insert, or delete a single record at a time. This control displays each field of a record on its own line. Sometimes .net developers use DetailsView in a master detail page.

Typically this control is used for updating and inserting records. DetailsView updating, inserting, and deleting records tasks depend on the data source control capabilities. DetailsView does not support sorting. This control supports paging. DetailsView creates a user interface for paging if we set the AllowPaging property value to true.

The .net developers can populate a DetailsView with data using many data sources control such as SqlDataSource, LinqDataSource, and ObjectDataSource control. DetailsView provides a UI that allows users to update a bound record. An editable view displays an additional row that contains Edit, Insert, and Delete command buttons.

After clicking a command button DetailsView redisplays the row with controls that allow users to modify the row contents. DetailsView display data automatically when AutoGenerateRows property value is set to true. DetailsView control uses textboxes to display data in a BoundField. DetailsView renders CheckBox to display Boolean data. Developers can customize input control displayed in edit mode by using a TemplateField.

The DetailsView inserts data by passing the values to be inserted in the data source using Values dictionary collection.

DetailsView PagerSettings property allows us to customize the appearance of the paging user interface. The Pager supports display modes. We can control it with the PagerSettings Mode property. Possible modes are NextPrevious, NextPreviousFirstLast, Numeric, and NumericFirstLast.

We can enable DetailsView built-in edit, insert and delete functionality by setting AutoGenerateEditButton, AutoGenerateInsertButton, and AutoGenerateDeleteButton properties values to true.

TemplateField enables developers to specify templates that contain markup and controls to customize the layout of a row in DetailsView. We can use ItemTemplate, InsertItemTemplate, and EditItemTemplate to customize the data insert and edit interface. In a template, we can bind controls to data using Eval and Bind methods.

The following c# example source code demonstrates to us how can we use DetailsView server control in the asp.net environment.
DetailsViewExample.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net DetailsView example: how to use DetailsView</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">DetailsView Example</h2>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT ProductID, ProductName, UnitPrice FROM Products">
        </asp:SqlDataSource>
        <asp:DetailsView ID="DetailsView" runat="server" DataSourceID="SqlDataSource1" AllowPaging="true" ForeColor="DarkGreen" BackColor="Snow" BorderColor="Tomato">
        </asp:DetailsView>
    </div>
    </form>
</body>
</html>