在ASP.NET的GridView(CommandField、ButtonField、ItemTemplate)裡的刪除Button加入Confirm的用法
最近在小舖看到有關這方面的問題.....
去網路上找了一下資料...就分享給大家吧...
c#範例
GridViewConfirm.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewConfirm.aspx.cs" Inherits="GridViewConfirm" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04
05 <html xmlns="http://www.w3.org/1999/xhtml" >
06 <head runat="server">
07 <title>GridViewConfirm</title>
08 </head>
09 <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>
41
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04
05 <html xmlns="http://www.w3.org/1999/xhtml" >
06 <head runat="server">
07 <title>GridViewConfirm</title>
08 </head>
09 <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>
41
GridViewConfirm.aspx.cs
01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 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 }
35
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 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 }
35
執行結果:
參考網址:
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20080313175621HB7&fumcde=FUM20041006161839LRJ&rplcnt=2
http://topic.csdn.net/u/20070820/16/91df12bb-3b7e-4060-bdaa-d8b4b77d02e9.html