<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="CommandField.aspx.cs" Inherits="CommandField" %><!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 id="Head1" runat="server">
    <title>数据连接</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="gdb1" runat = "server" AutoGenerateColumns="False" BackColor="White"
          BorderColor ="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3"
          DataSourceID="SqlDataSource1" Font-Size="10pt" 
            OnRowDeleting="gviewProduct_RowDeleting" Width="408px">
          <FooterStyle BackColor ="#B5C7DE" ForeColor ="#4A3C8C" />
          <Columns>
               <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
                   ReadOnly="True" SortExpression="ProductID" />
               <asp:BoundField DataField="ProductName" HeaderText="ProductName"  SortExpression="ProductName" />
               <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"  SortExpression="UnitPrice" />
               <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"  SortExpression="UnitsInStock" />
               <asp:CommandField ShowCancelButton ="true" />
               <asp:CommandField  ShowCancelButton ="True" />
               <asp:CommandField ShowCancelButton ="True" />
               </Columns>
               <RowStyle BackColor ="#E7E7FF" ForeColor ="#4A3C8C" />
               <SelectedRowStyle BackColor ="#738A9C" Font-Bold ="True" ForeColor ="#F7F7F7" />
               <PagerStyle BackColor ="#E7E7FF" ForeColor ="#4A3C8C" HorizontalAlign ="Right" />
               <HeaderStyle BackColor ="#4A3C8C" Font-Bold ="True" ForeColor ="#F7F7F7" />
               <AlternatingRowStyle  BackColor  ="#F7F7F7" />
                
    </asp:GridView>      
    </div>
    <asp:SqlDataSource ID ="SqlDataSourcel" runat ="server" ConflictDetection ="CompareAllValues"
          ConnectionString ="%$ ConnectionStrings:NorthwindConnectinString%" DeleteCommand ="DELETE FROM [Products] WHERE [ProductID]= @original_ProductID
          AND [ProductName]=@original_ProductName AND [UnitPrice]=@original_UnitPrice AND [UnitsInStock]=@original_UnitsInStock"
          InsertCommand="INSERT INTO [Products]([ProductName],[UnitPrice],[UnitsInStock]) VALUES (@ProductName,@UnitPrice,@UnitsInStock)"
          OldValuesParameterFormatString="original_{0}"
          UpdateCommand="UPDATE [Products] SET [ProductName]=@ProductName,[UnitPrice]=@UnitPrice,[UnitsInStock]=@UnitsInStock WHERE [ProductID]=@original_ProductID AND [ProductName]=@original_ProductName
          AND [UnitPrice]=@original_UnitPrice AND [UnitsInStock]=@original_UnitsInStock" SelectCommand="SELECT [ProductID],[ProductName],[UnitPrice],[UnitsInStock] FROM [Products]">
          <DeleteParameters>
                <asp:Parameter Name ="original_ProductID" Type ="Int32" />
                <asp:Parameter Name ="original_ProductName" Type ="String" />
                <asp:Parameter Name ="original_UnitPrice" Type ="Decimal" />
                <asp:Parameter Name ="original_UnitsInStock" Type ="Int16" />
           </DeleteParameters>
           <UpdateParameters >
                <asp:Parameter Name="ProductName" Type="String" />
                <asp:Parameter Name="UnitPrice" Type="Decimal" />
                <asp:Parameter Name="UnitsInStock" Type="Int16" />
                <asp:Parameter Name ="original_ProductID" Type ="Int32" />
                <asp:Parameter Name ="original_ProductName" Type ="String" />
                <asp:Parameter Name ="original_UnitPrice" Type ="Decimal" />
                <asp:Parameter Name ="original_UnitsInStock" Type ="Int16" />
           </UpdateParameters>
           <InsertParameters >
                <asp:Parameter Name="ProductName" Type="String" />
                <asp:Parameter Name="UnitPrice" Type="Decimal" />
                <asp:Parameter Name="UnitsInStock" Type="Int16" />
           </InsertParameters>
        </asp:SqlDataSource>    
    </form>
</body>
</html>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;public partial class CommandField : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void gviewProduct_RowDeleting(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        Literal txtMsg = new Literal();
        txtMsg.Text = "<script>alert('数据行删除取消')</script>";
        Page.Controls.Add(txtMsg);
    }
}