分享

FormView控件

 行走在理想边缘 2019-02-15

FormView控件是ASP.NET 2.0工具箱引入的,它的工作方式类似于DetailsView控件,也是显示绑定数据源控件中的一个数据项,并可以添加、编辑和删除数据。它比较独特的 地方是在定制模板中显示数据,可以更多地控制数据的显示和编辑方式。图7-35显示了在Visual Studio中编辑的FormView控件ItemTemplate。从中可以看出,我们能完全控制数据的显示方式。FormView控件还包含 EditItemTemplate和InsertItemTemplate,它可以确定控件在进入编辑或插入模式下的显示方式。

 
(点击查看大图)图  7-35

图7-35显示了Visual Studio中的FormView控件。图7-36显示了FormView控件的ItemTemplate,表示在Visual Studio中设计的定制布局。

 
图  7-36

在图7-37中,该控件处于编辑模式,显示了标准的EditItemTemplate布局。

 
图  7-37

程序清单7-58是在设计FormView控件的定制ItemTemplate时Visual Studio生成的代码。

程序清单7-58  使用FormView控件显示和编辑数据

<%@ 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>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多