One Record at a Time: DetailsView

With lists in hand, let’s expand your frame of data view from a couple of fields per row to an entire row. ASP.NET v2 introduced two new controls, the DetailsView and the FormView, to work with one row of information at a time. The DetailsView, which you’ll look at now, applies a structure to the presentation of this information—it places it in the rows and columns of an HTML table—whereas the FormView, which you’ll look at later, removes that structure and asks you to define your own.

To demonstrate the basic workings of the DetailsView, add to the C8_DataAccess website a new page called DetailsView.aspx and add a SqlDataSource to it that retrieves all the information from the Customer table. Then, add a DetailsView control to the page and use its Common Tasks panel to connect it to the data source. Example 8-3 shows the resultant markup.

Example 8-3. DetailsView.aspx initial markup

<%@ Page Language="C#" AutoEventWireup="true"
   CodeFile="DetailsView.aspx.cs" Inherits="DetailsView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>DetailsView Page</title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
      <asp:SqlDataSource ID="dsCustomers" runat="server"
         ConnectionString=
            "<%$ ConnectionStrings:AWLTConnection%>"
         SelectCommand="SELECT * FROM [SalesLT].[Customer]" />
 <asp:DetailsView ID="DetailsView1" ...

Get Programming ASP.NET 3.5, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.