源代码:13033480群共享
FormView可分页呈现一个表格的数据,每页只呈现表格中的一项。它的最大特点是可自由编辑模板,通常在主/详细方案中使用,用来显示商品的详细信息。
|
1、设置FormView控件,注意DataKeyNames="ItemID"项。
2、设置SqlDataSource属性,由于要查询两个内联表,两个表中都有一个Name字段,因此用了别名。
3、编辑ItemTemplate模板,先添加了“编辑”、“删除”、“新建”按钮,“编辑”和“新建”按钮都有个CommandName属性,分别为Edit和New,点击可分别进入EditItemTemplate和InsertItemTemplate、模板;删除按钮的CommandName属性是Delete,点击可执行SqlDataSource中的DeleteCommand命令,同时,还可发出OnItemDeleting等命令,在删除前完成一些功能。
4、窗体文件代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FormViewDemo1.aspx.cs" Inherits="FormViewDemo1" %>
<!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>肯德基订餐系统</title>
</head>
<body>
<form id="form1" runat="server">
<h3>FormView 显示、更新、插入、删除数据库操作</h3>
<asp:FormView ID="fvwItem" DataSourceID="sdsItem" runat="server"
AllowPaging="True"
DataKeyNames="ItemID"
EmptyDataText="数据库中暂时没有任何数据">
<RowStyle BackColor="Yellow" Wrap="False" />
<InsertRowStyle BackColor="GreenYellow" Wrap="False" />
<EditRowStyle BackColor="LightPink" Wrap="false" />
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="420">
<tr>
<td colspan="6" height="30" width="420" align="center">
<h4>FormView ItemTemplate 模板</h4>
</td>
</tr>
<tr>
<td width="30">
</td>
<td rowspan="4" width="120">
<asp:Image Width="120" Height="120" ID="imgItem" ImageUrl='<%# Eval("Image") %>' AlternateText='<%# Eval("Name") %>'
runat="server" /></td>
<td width="30">
</td>
<td width="60">
</td>
<td width="60">
</td>
<td width="60">
</td>
<td width="60">
</td>
</tr>
<tr>
<td width="30">
</td>
<td width="30">
</td>
<td width="60">
类别:</td>
<td colspan="2">
<%# Eval("CategoryName") %>
</td>
<td width="60">
</td>
</tr>
<tr>
<td width="30">
</td>
<td width="30">
</td>
<td width="60">
名称:</td>
<td colspan="2">
<%# Eval("Name") %>
</td>
<td width="60">
</td>
</tr>
<tr>
<td width="30">
</td>
<td width="30">
</td>
<td width="60">
价格:
</td>
<td colspan="2">
<%# Eval("Price") %>
</td>
<td width="60">
</td>
</tr>
<tr>
<td height="30" width="30">
</td>
<td height="30" width="120">
</td>
<td height="30" width="30">
</td>
<td height="30" width="60">
</td>
<td height="30" width="60">
<asp:Button ID="btnEdit" runat="server" Text="编辑" CommandName="Edit"/></td>
<td height="30" width="60">
<asp:Button ID="btnDelete" runat="server" Text="删除" CommandName="Delete"/></td>
<td height="30" width="60">
<asp:Button ID="btnNew" runat="server" Text="新建" CommandName="New" /></td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="sdsItem" runat="server" ConnectionString="<%$ ConnectionStrings:NetShopConnString %>"
SelectCommand="SELECT Item.ItemId AS ItemId,Item.CategoryId AS CategoryId,Item.Name AS Name,Item.Price AS Price,Item.Image AS Image,Category.Name As CategoryName FROM Item INNER JOIN Category ON Item.CategoryId=Category.CategoryId">
</asp:SqlDataSource>
</form>
</body>
</html>
5、代码页不需要任何代码,可直接在浏览器查看运行情图,如图1示。
参考网址:http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.formview%28v=VS.80%29.aspx