Introduction
We use the FormView control to do anything that we also can do with the
DetailsView control. Just as we can with the DetailsView control, we can use the
FormView control to display, page, edit, insert, and delete database records.
However, unlike the DetailsView control, the FormView control is entirely
template driven. I end up using the FormView control much more than the
DetailsView control. The FormView control provides us with more control over the
layout of a form. Furthermore, adding validation controls to a FormView is
easier than adding validation controls to a DetailsView control.
Displaying Data with FormView Control
We can display a database record with the FormView control by using an
ItemTemplate. For example, the page given below displays a record from the
BOOK_LIST database table.
<%@
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
id="Head1"
runat="server">
<style
type="text/css">
html
{
background-color:silver;
}
#content
{
margin:auto;
width:300px;
padding:10px;
background-color:white;
font:14px
Georgia,Serif;
}
</style>
<title></title>
</head>
<body>
<form
id="form1"
runat="server">
<div
id="content">
<asp:FormView
id="frmBooks"
DataSourceID="SqlDataSource1"
Runat="server">
<ItemTemplate>
<b><u><%#
Eval("TITLE")%></u></b>
<br
/>
<b>Serial
Number:</b>
<%#
Eval("ID")%>
<br
/>
<b>Author:</b>
<%#
Eval("AUTHOR")%>
<br
/>
<b>Price:</b>
<%#
Eval("PRICE")%>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$
ConnectionStrings:DatabaseConnectionString1 %>"
ProviderName="<%$
ConnectionStrings:DatabaseConnectionString1.ProviderName
%>"
SelectCommand="SELECT
[ID], [TITLE], [AUTHOR], [PRICE] FROM [BOOK_LIST]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
In above code, FormView control's DataSourceID property points to the
SqlDataSource control. The SqlDataSource control retrieves the first record from
the Movies database table.
Notice that the ItemTemplate contains databinding expressions that display the
values of the TITLE, ID, AUTHOR and PRICE columns. The Eval() method retrieves
the values of these columns. I have also used CSS for its better look.
Note: Continue in Next Part.
HAVE A GREAT CODING!