投递文章投递文章 投稿指南 RSS订阅RSS订阅

GridView+FormView 示范数据 新增/修改/删除

来源:cnblogs 发布时间:2008-05-11 收藏 投稿 字体:【

  摘要
在同一页面上以 GridView 配合 FormView 来完成数据的「新增/修改/删除」,在这个范例中有下列二个特点。
1. GridView 及 FormView 系结同一个 SqlDataSource 控件。
2. FormView 只使用 EditItemTemplate,同时来做新增及修改的动作。
范例程序代码: GridView1.rar

画面配置
此范例使用 Northwind 数据库的 Employees 数据表当作数据来源,在页面上放置 SqlDataSource、GridView、FormView,而 GridView 及 FormView 系结至同一个 SqlDataSource 控件。
GridView 的部分,启用分页模式,每页设为 5 笔,并将 CommandField 转为 TemplateField,然后在 HeaderTemplate 部分加入一个「新增」钮。
FormView 的部分在浏览模式为隐藏,设定属性 Visible="False"。执行新增及编辑时只会使用到 EditItemTemplate,故只保留 EditItemTemplate 的内容,然后设定属性 DefaultMode="Edit"。
 

aspx 程序代码如下
Code
  1<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
  2
  3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4
  5<html xmlns="http://www.w3.org/1999/xhtml" >
  6<head runat="server">
  7    <title>未命名页面</title>
  8</head>
  9<body>
 10    <form id="form1" runat="server">
 11    <div style="text-align: center">
 12        <strong><span style="color: #3333ff">GridView+FormView 示范数据 新增/修改/删除<br />
 13        </span></strong>
 14        <br />
 15        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
 16            CellPadding="4" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1" EmptyDataText="没有数据录可显示。"
 17            ForeColor="#333333" GridLines="None" PageSize="5">
 18            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
 19            <RowStyle BackColor="#EFF3FB" />
 20            <Columns>
 21                <asp:TemplateField ShowHeader="False">
 22                    <HeaderTemplate>
 23                        <asp:Button ID="btnInsert" runat="server" CausesValidation="False" CommandName="Insert"
 24                            Text="新增"></asp:Button>
 25                    </HeaderTemplate>
 26                    <ItemTemplate>
 27                        <asp:Button ID="btnEdit" runat="server" CausesValidation="False" CommandName="Edit"
 28                            Text="编辑"></asp:Button>
 29                        <asp:Button ID="btnDelete" runat="server" CausesValidation="False" CommandName="Delete"
 30                            Text="删除"></asp:Button>
 31                    </ItemTemplate>
 32                </asp:TemplateField>
 33                <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False"
 34                    ReadOnly="True" SortExpression="EmployeeID" />
 35                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
 36                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
 37                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
 38            </Columns>
 39            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
 40            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
 41            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
 42            <EditRowStyle BackColor="#2461BF" />
 43            <AlternatingRowStyle BackColor="White" />
 44        </asp:GridView>
 45        <asp:FormView ID="FormView1" runat="server" CellPadding="4" DataKeyNames="EmployeeID"
 46            DataSourceID="SqlDataSource1" DefaultMode="Edit" ForeColor="#333333" Visible="False">
 47            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
 48            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
 49            <EditItemTemplate>
 50                <table style="width: 400px">
 51                    <tr>
 52                        <td style="width: 100px; text-align: left">
 53                            EmployeeID</td>
 54                        <td style="width: 392px; text-align: left">
 55                            <asp:Label ID="EmployeeIDLabel1" runat="server" Text='<%# Eval("EmployeeID") %>'></asp:Label>
 56                            <asp:TextBox ID="txtEmployeeID" runat="server" Text='<%# Bind("EmployeeID") %>'></asp:TextBox></td>
 57                    </tr>
 58                    <tr>
 59                        <td style="width: 100px; text-align: left">
 60                            LastName</td>
 61                        <td style="width: 392px; text-align: left">
 62                            <asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox></td>
 63                    </tr>
 64                    <tr>
 65                        <td style="width: 100px; text-align: left">
 66                            FirstName</td>
 67                        <td style="width: 392px; text-align: left">
 68                            <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox></td>
 69                    </tr>
 70                    <tr>
 71                        <td style="width: 100px; text-align: left">
 72                            Title</td>
 73                        <td style="width: 392px; text-align: left">
 74                            <asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox></td>
 75                    </tr>
 76                </table>
 77                <br />
 78                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="False" CommandName="Insert"
 79                    Text="新增"></asp:LinkButton>
 80                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
 81                    Text="更新"></asp:LinkButton>
 82                <asp:LinkButton ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
 83                    Text="取消"></asp:LinkButton>
 84            </EditItemTemplate>
 85            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
 86            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
 87        </asp:FormView>
 88        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>"
 89            DeleteCommand="DELETE FROM [Employees] WHERE [EmployeeID] = @original_EmployeeID" InsertCommand="INSERT INTO [Employees] ([LastName], [FirstName], [Title]) VALUES (@LastName, @FirstName, @Title)"
 90            ProviderName="<%$ ConnectionStrings:NorthwindConnectionString1.ProviderName %>"
 91            SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Title] FROM [Employees]"
 92            UpdateCommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName, [Title] = @Title WHERE [EmployeeID] = @original_EmployeeID" OldValuesParameterFormatString="original_{0}">
 93            <DeleteParameters>
 94                <asp:Parameter Name="original_EmployeeID" Type="Int32" />
 95            </DeleteParameters>
 96            <InsertParameters>
 97                <asp:Parameter Name="LastName" Type="String" />
 98                <asp:Parameter Name="FirstName" Type="String" />
 99                <asp:Parameter Name="Title" Type="String" />
100            </InsertParameters>
101            <UpdateParameters>
102                <asp:Parameter Name="LastName" Type="String" />
103                <asp:Parameter Name="FirstName" Type="String" />
104                <asp:Parameter Name="Title" Type="String" />
105                <asp:Parameter Name="original_EmployeeID" Type="Int32" />
106            </UpdateParameters>
107        </asp:SqlDataSource>
108        <br />
109   
110    </div>
111    </form>
112</body>
113</html>

顶一下
(0)
0%
踩一下
(0)
0%
本文Tags:
  • 表情:
  •    
  • 评价:
用户名: 密码: 匿名 注册
最新评论 查看所有评论
About iTtang - 联系我们  - 专题列表 - 友情链接  -  高级搜索  -  帮助中心  -  您的意见