FormView 显示、更新、插入、删除数据库操作[ASP.NET源代码](一)

源代码:13033480群共享

FormView可分页呈现一个表格的数据,每页只呈现表格中的一项。它的最大特点是可自由编辑模板,通常在主/详细方案中使用,用来显示商品的详细信息。
FormView有三个可编辑模板,ItemTemplate、EditItemTemplate和InsertItemTemplate、常用来管理数据库表格数据,显示、更新、插入、删除表格中的数据项。
FormView 控件提供了两种用于绑定到数据的选项:

  • 使用 DataSourceID 属性进行数据绑定,此选项使您能够将 FormView 控件绑定到数据源控件。它允许 FormView 控件利用数据源控件的功能并提供了内置的更新和分页功能。
  • 使用 DataSource 属性进行数据绑定,此选项使您能够绑定到包括 ADO.NET 数据集和数据读取器在内的各种对象。此方法需要您为任何附加功能(如更新、插入、删除和分页等)编写代码。

    :

一、使用 FormView控件显示 SqlDataSource控件中的值

1、设置FormView控件,注意DataKeyNames="ItemID"项。

2、设置SqlDataSource属性,由于要查询两个内联表,两个表中都有一个Name字段,因此用了别名。

3、编辑ItemTemplate模板,先添加了“编辑”、“删除”、“新建”按钮,“编辑”和“新建”按钮都有个CommandName属性,分别为EditNew,点击可分别进入EditItemTemplateInsertItemTemplate、模板;删除按钮的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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值