FormView控件是ASP.NET
2.0工具箱引入的,它的工作方式類似于DetailsView控件,也是顯示綁定數(shù)據(jù)源控件中的一個(gè)數(shù)據(jù)項(xiàng),并可以添加、編輯和刪除數(shù)據(jù)。它比較獨(dú)特的
地方是在定制模板中顯示數(shù)據(jù),可以更多地控制數(shù)據(jù)的顯示和編輯方式。圖7-35顯示了在Visual
Studio中編輯的FormView控件ItemTemplate。從中可以看出,我們能完全控制數(shù)據(jù)的顯示方式。FormView控件還包含
EditItemTemplate和InsertItemTemplate,它可以確定控件在進(jìn)入編輯或插入模式下的顯示方式。
|
(點(diǎn)擊查看大圖)圖 7-35 |
圖7-35顯示了Visual Studio中的FormView控件。圖7-36顯示了FormView控件的ItemTemplate,表示在Visual Studio中設(shè)計(jì)的定制布局。
|
圖 7-36 |
在圖7-37中,該控件處于編輯模式,顯示了標(biāo)準(zhǔn)的EditItemTemplate布局。
|
圖 7-37 |
程序清單7-58是在設(shè)計(jì)FormView控件的定制ItemTemplate時(shí)Visual Studio生成的代碼。
程序清單7-58 使用FormView控件顯示和編輯數(shù)據(jù)
<%@ Page Language="C#" %>
<html xmlns="http://www./1999/xhtml
" >
<head runat="server">
<title>Using the FormView control</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="FormView1" Runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="CustomerID" AllowPaging="True">
<EditItemTemplate>
CustomerID:
<asp:Label Text='<%# Eval("CustomerID") %>' Runat="server"
ID="CustomerIDLabel1">
</asp:Label><br />
CompanyName:
<asp:TextBox Text='<%# Bind("CompanyName") %>' Runat="server"
ID="CompanyNameTextBox"></asp:TextBox><br />
ContactName:
<asp:TextBox Text='<%# Bind("ContactName") %>' Runat="server"
ID="ContactNameTextBox"></asp:TextBox><br />
ContactTitle:
<asp:TextBox Text='<%# Bind("ContactTitle") %>' Runat="server"
ID="ContactTitleTextBox"></asp:TextBox><br />
Address:
<asp:TextBox Text='<%# Bind("Address") %>' Runat="server"
ID="AddressTextBox"></asp:TextBox><br />
City:
<asp:TextBox Text='<%# Bind("City") %>' Runat="server"
ID="CityTextBox"></asp:TextBox><br />
Region:
<asp:TextBox Text='<%# Bind("Region") %>' Runat="server"
ID="RegionTextBox"></asp:TextBox><br />
PostalCode:
<asp:TextBox Text='<%# Bind("PostalCode") %>' Runat="server"
ID="PostalCodeTextBox"></asp:TextBox><br />
Country:
<asp:TextBox Text='<%# Bind("Country") %>' Runat="server"
ID="CountryTextBox"></asp:TextBox><br />
Phone:
<asp:TextBox Text='<%# Bind("Phone") %>' Runat="server"
ID="PhoneTextBox"></asp:TextBox><br />
Fax:
<asp:TextBox Text='<%# Bind("Fax") %>' Runat="server"
ID="FaxTextBox"></asp:TextBox><br />
<br />
<asp:Button ID="Button2" Runat="server" Text="Button"
CommandName="update" />
<asp:Button ID="Button3" Runat="server" Text="Button"
CommandName="cancel" />
</EditItemTemplate>
<ItemTemplate>
<table width="100%">
<tr>
<td style="width: 439px">
<b>
<span style="font-size: 14pt">Customer Information</span>
</b>
</td>
<td style="width: 439px" align="right">
CustomerID:
<asp:Label ID="CustomerIDLabel" Runat="server"
Text='<%# Bind("CustomerID") %>'>
</asp:Label></td>
</tr>
<tr>
<td colspan="2">
CompanyName:
<asp:Label ID="CompanyNameLabel" Runat="server"
Text='<%# Bind("CompanyName") %>'>
</asp:Label><br />
ContactName:
<asp:Label ID="ContactNameLabel" Runat="server"
Text='<%# Bind("ContactName") %>'>
</asp:Label><br />
ContactTitle:
<asp:Label ID="ContactTitleLabel" Runat="server"
Text='<%# Bind("ContactTitle") %>'>
</asp:Label><br />
<br />
<table width="100%"><tr>
<td colspan="3">
<asp:Label ID="AddressLabel" Runat="server"
Text='<%# Bind("Address") %>'>
</asp:Label></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Label ID="CityLabel" Runat="server"
Text='<%# Bind("City") %>'>
</asp:Label></td>
<td style="width: 100px">
<asp:Label ID="RegionLabel" Runat="server"
Text='<%# Bind("Region") %>'>
</asp:Label></td>
<td style="width: 100px">
<asp:Label ID="PostalCodeLabel"
Runat="server"
Text='<%# Bind("PostalCode") %>'>
</asp:Label>
</td>
</tr>
<tr>
<td style="width: 100px" valign="top">
<asp:Label ID="CountryLabel" Runat="server"
Text='<%# Bind("Country") %>'>
</asp:Label></td>
<td style="width: 100px"></td>
<td style="width: 100px">
Phone:
<asp:Label ID="PhoneLabel" Runat="server"
Text='<%# Bind("Phone") %>'>
</asp:Label><br />
Fax:
<asp:Label ID="FaxLabel" Runat="server"
Text='<%# Bind("Fax") %>'>
</asp:Label><br />
</td>
</tr></table>
<asp:Button ID="Button1" Runat="server"
Text="Button" CommandName="edit" />
</td>
</tr></table>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
SelectCommand="SELECT * FROM [Customers]"
ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
|