zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    PHP四种界定符
    设计模式 单例模式与工厂模式
    PHP include与require的区别
    面向对象 static abstract interface 等知识点
    gogland golang 颜色&字体 colors&font 配置文件
    什么是游戏中的帧同步
    kcp协议详解
    kcp流模式与消息模式对比
    kcp源码走读
    kcp结构体字段含义
  • 原文地址:https://www.cnblogs.com/java20130723/p/3211716.html
Copyright © 2011-2022 走看看