DetailsView Control in ASP.NET - Part 2
Introduction
In Part 1 of this article series we have discussed how to display the data
in DetailsView Control. We have discussed Declarative and Programmatic Databindng technique. Now in this
article we will discuss how to use fields in DetailsView Control.
Using Fields with DetailsView
If we need more control over the appearance of the DetailsView, including the
particular order in which columns are displayed, then we can use fields with
the DetailsView control.
The DetailsView control
supports exactly the same fields as the GridView control:
- BoundField: Enables us to
display the value of a data item as text.
- CheckBoxField: Enables us to
display the value of a data item as a check box.
- CommandField: Enables us to
display links for editing, deleting, and selecting rows.
- ButtonField: Enables us to
display the value of a data item as a button (image button, link button,
or push button).
- HyperLinkField: Enables us to
display the value of a data item as a link.
- ImageField: Enables us to
display the value of a data item as an image.
- TemplateField: Enables us to
customize the appearance of a data item.
Another option is to create custom fields for the DetailsView control. We can create
custom fields that work with the DetailsViewcontrol
in exactly the same way as we create custom fields that work with the GridView control (we have already
discussed on it earlier in GridView Control
article Parts).
The page given below contains a DetailsView control
that contains three BoundFields. The BoundFields display the values of the
ID, TITLE, COMPANY, PRICE.
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView
id="dtlProducts"
DataSourceID="SqlDataSource1"
AutoGenerateRows="false"
Runat="server">
<Fields>
<asp:BoundField
DataField="ID"
HeaderText="Product Number:" />
<asp:BoundField
DataField="TITLE"
HeaderText="Product Name:" />
<asp:BoundField
DataField="PRICE"
DataFormatString="{0:c}"
HtmlEncode="false"
HeaderText="Market Price:" />
</Fields>
</asp:DetailsView>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
SelectCommand="SELECT [ID], [TITLE],
[COMPANY], [PRICE] FROM [PRO_LIST]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
In above example DetailsView control
has an AutoGenerateRows property
that has the value False. When we specify fields for aDetailsView control, we include this property so that the fields do not
appear more than once. Each of the BoundFields includes
aHeaderText attribute
that is used to specify the label for the field. In addition, the BoundField associated with the PRICE column includes a DataFormatString property that is used to format the value of the column as a
currency amount. Make
sure that we disabled HTML encoding by setting the HtmlEncode property to the value False when we
use the DataFormatString property.
Note: Continue in Next Part.
Comments
Post a Comment