您的位置:首页 > 编程语言 > ASP

在ASP.NET的GridView(CommandField、ButtonField、ItemTemplate)裡的刪除Button加入Confirm的用法

2008-07-31 17:07 513 查看
c#范例: GridViewConfirm.aspx

<%@ 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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: