在ASP.NET的GridView(CommandField、ButtonField、ItemTemplate)里的刪除Button加入Confirm的用法 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewConfirm.aspx.cs" Inherits="GridViewConfirm" %> 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>GridViewConfirm</title> 8 </head> 9 <body> 10 <form id="form1" runat="server"> 11 <div> 12 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" 13 DataSourceID="SqlDataSource1" EmptyDataText="没有资料可显示。" OnRowCreated="GridView1_RowCreated"> 14 <Columns> 15 <asp:CommandField ButtonType="Button" HeaderText="CommandField" ShowDeleteButton="True" /> 16 <asp:ButtonField ButtonType="Button" HeaderText="ButtonField" Text="刪除" CommandName="Delete" /> 17 <asp:TemplateField HeaderText="TemplateField"> 18 <ItemTemplate> 19 <asp:Button ID="Button1" runat="server" OnClientClick="return confirm('确定删除吗?')" 20 Text="刪除" CommandName="Delete" /> 21 </ItemTemplate> 22 </asp:TemplateField> 23 <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" /> 24 <asp:BoundField DataField="gender" HeaderText="gender" SortExpression="gender" /> 25 <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" /> 26 </Columns> 27 </asp:GridView> 28 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" 29 DeleteCommand="DELETE FROM [user] WHERE [id] = @id" 30 ProviderName="<%$ ConnectionStrings:DatabaseConnectionString.ProviderName %>" 31 SelectCommand="SELECT [id], [gender], [name] FROM [user]" > 32 <DeleteParameters> 33 <asp:Parameter Name="id" Type="Int32" /> 34 </DeleteParameters> 35 </asp:SqlDataSource> 36 37 </div> 38 </form> 39 </body> 40 </html> GridViewConfirm.aspx.cs 1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Collections; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.WebControls; 9 using System.Web.UI.WebControls.WebParts; 10 using System.Web.UI.HtmlControls; 11 12 public partial class GridViewConfirm : System.Web.UI.Page 13 { 14 protected void Page_Load(object sender, EventArgs e) 15 { 16 17 } 18 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 19 { 20 //此是要判断为DataRow才执行,因为GridView有Header,Footer...等 21 if (e.Row.RowType == DataControlRowType.DataRow) 22 { 23 //CommandField使用此方法 24 ((Button)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "if(!window.confirm('确定删除吗?'')) return;"); 25 26 27 //ButtonField使用此方法 28 ((Button)e.Row.Cells[1].Controls[0]).Attributes.Add("onclick", "if(!window.confirm('确定删除吗?'')) return;"); 29 30 31 //TemplateField已经在.aspx使用 OnClientClick="return confirm('确定删除吗?')" 设定了 32 } 33 } 34 } 下面贴上OJ该部分源代码: //前台 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" DataSourceID="SqlDataSource1" Width="593px" CellPadding="4" ForeColor="#333333" GridLines="None" DataKeyNames="ID"> <RowStyle BackColor="#EFF3FB" /> <Columns> <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> <asp:TemplateField HeaderText="Filename" SortExpression="Filename" Visible="False"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Filename") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Filename") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Classes" HeaderText="Classes" SortExpression="Classes" /> <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" /> <asp:BoundField DataField="AddTime" HeaderText="AddTime" SortExpression="AddTime" /> <asp:TemplateField HeaderText="删除"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" OnClientClick="return confirm('确定删除吗?')" CommandName="Delete" Text="删除"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <EmptyDataTemplate> 暂无此类资源 </EmptyDataTemplate> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#2461BF" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> //后台 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { //string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; int algoid = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()); AlgorithmBLL algoBLL=new AlgorithmBLL (); new AlgorithmBLL().DeleteByID(algoid); GridView1.DataBind(); }