积极答复者
求助:关于gridview的CommandField添加含数据列内容的删除确认提示的问题

问题
-
之前的帖子代码不全导致数位热心帮我的大师走了不少冤枉路,现在整理下从新发帖。
需注意的问题:
求助的问题1:如何使用<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ButtonType="Button" />中的删除按钮实现的'你确认要删除:" + 名称 + "?删除确认提示。(不添加新列或生产新按钮)
求助的问题2:使用模板列中asp:Button ID="Button4"如何实现删除按钮实现的'你确认要删除:" + 名称 + "?删除确认提示。Language="C#"
<asp:GridView ID="GridView" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" DataSourceID="ObjectDataSourceshujubianji" ForeColor="#333333" GridLines="None" OnRowDeleting="GridView_RowDeleting" OnRowDataBound ="GridView_RowDataBound" > <AlternatingRowStyle BackColor="White" /> <Columns> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ButtonType="Button" /> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="栏目" HeaderText="栏目" SortExpression="栏目" /> <asp:BoundField DataField="分类" HeaderText="分类" SortExpression="分类" /> <asp:BoundField DataField="名称" HeaderText="名称" SortExpression="名称" /> <asp:BoundField DataField="作者" HeaderText="作者" SortExpression="作者" /> <asp:BoundField DataField="审核" HeaderText="审核" SortExpression="审核" /> <asp:BoundField DataField="备注" HeaderText="备注" SortExpression="备注" /> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:Button ID="Button4" runat="server" CommandName="delete" Text="Button" /> </ItemTemplate> </asp:TemplateField> </Columns> <EditRowStyle BackColor="#2461BF" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#EFF3FB" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#F5F7FB" /> <SortedAscendingHeaderStyle BackColor="#6D95E1" /> <SortedDescendingCellStyle BackColor="#E9EBEF" /> <SortedDescendingHeaderStyle BackColor="#4870BE" /> </asp:GridView> ==================================================================================== protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string q = e.Row.Cells[4].Text; Button btn = e.Row.Cells[0].Controls[1] as Button; btn.Text = "删除:'" + q; btn.OnClientClick = "if (confirm('你确认要删除:" + q + "?')) javascript:__doPostBack('GridView','Delete$" + e.Row.RowIndex.ToString() + "'); else return false;"; } <br/>
绝不不懂装懂,不因为自己的问题低级而感到不好意思,踏踏实实的虚心学习。
答案
-
若要在非CommandFiled自訂Delete按鈕,可依照下列步驟進行:(以Button為例)
1. 設定Button的CommandName為Delete。
<asp:TemplateField> <ItemTemplate> <asp:Button ID="Button4" runat="server" CommandName="delete" onclick="Button4_Click" Text="Button" /> </ItemTemplate>
2.在GridView的RowDataBind事件加入刪除確認的提示訊息。protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string q = e.Row.Cells[1].Text; Button btn = e.Row.Cells[4].Controls[1] as Button; btn.Text = "删除:'" + q; btn.OnClientClick = "return confirm('你确认要删除:" + q + "?');"; } }
下圖為執行結果,請依照您的情境調整Cells的index。完整程式碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GV.aspx.cs" Inherits="WebApplication1.GV" %> <!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 runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="RegionID" DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound" > <Columns> <asp:CommandField ButtonType="Button" HeaderText="Delete" ShowDeleteButton="True" /> <asp:BoundField DataField="RegionID" HeaderText="RegionID" ReadOnly="True" SortExpression="RegionID" /> <asp:BoundField DataField="RegionDescription" HeaderText="RegionDescription" SortExpression="RegionDescription" /> <asp:BoundField DataField="RegionID" HeaderText="Delete" ReadOnly="True" SortExpression="RegionID" /> <asp:TemplateField> <ItemTemplate> <asp:Button ID="Button4" runat="server" CommandName="delete" onclick="Button4_Click" Text="Button" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT * FROM [Region]" DeleteCommand="DELETE FROM [Region] WHERE [RegionID] = @RegionID" InsertCommand="INSERT INTO [Region] ([RegionID], [RegionDescription]) VALUES (@RegionID, @RegionDescription)" UpdateCommand="UPDATE [Region] SET [RegionDescription] = @RegionDescription WHERE [RegionID] = @RegionID"> <DeleteParameters> <asp:Parameter Name="RegionID" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="RegionID" Type="Int32" /> <asp:Parameter Name="RegionDescription" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="RegionDescription" Type="String" /> <asp:Parameter Name="RegionID" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> </div> </form> </body> </html>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string q = e.Row.Cells[1].Text; Button btn = e.Row.Cells[4].Controls[1] as Button; btn.Text = "删除:'" + q; btn.OnClientClick = "return confirm('你确认要删除:" + q + "?');"; } }
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/ -
Shadow And Happy Code 提供原生delete按钮的源码
以下是根據您的需求重新寫過一遍的代碼
CommandField我拆成編輯、刪除兩欄了
<%@ Page Debug="true" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %> <!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 runat="server"> </head> <body> <form id="form1" runat="server"> <asp:SqlDataSource runat="server" ID="ObjectDataSourceshujubianji" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [shujubianji]" DeleteCommand="DELETE FROM [shujubianji] WHERE [ID] = @ID" InsertCommand="INSERT INTO [shujubianji] ([栏目], [分类], [名称], [作者], [审核], [备注]) VALUES (@栏目, @分类, @名称, @作者, @审核, @备注)" UpdateCommand="UPDATE [shujubianji] SET [栏目] = @栏目, [分类] = @分类, [名称] = @名称, [作者] = @作者, [审核] = @审核, [备注] = @备注 WHERE [ID] = @ID" > <DeleteParameters> <asp:Parameter Name="ID" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="栏目" Type="String" /> <asp:Parameter Name="分类" Type="String" /> <asp:Parameter Name="名称" Type="String" /> <asp:Parameter Name="作者" Type="String" /> <asp:Parameter Name="审核" Type="String" /> <asp:Parameter Name="备注" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="栏目" Type="String" /> <asp:Parameter Name="分类" Type="String" /> <asp:Parameter Name="名称" Type="String" /> <asp:Parameter Name="作者" Type="String" /> <asp:Parameter Name="审核" Type="String" /> <asp:Parameter Name="备注" Type="String" /> <asp:Parameter Name="ID" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> <asp:GridView ID="GridView" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" DataSourceID="ObjectDataSourceshujubianji" ForeColor="#333333" GridLines="None" OnRowDataBound ="GridView_RowDataBound" > <AlternatingRowStyle BackColor="White" /> <Columns> <asp:CommandField ShowEditButton="True" ButtonType="Button" HeaderText="編輯" /> <asp:CommandField ButtonType="Button" HeaderText="刪除" ShowDeleteButton="True" /> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="栏目" HeaderText="栏目" SortExpression="栏目" /> <asp:BoundField DataField="分类" HeaderText="分类" SortExpression="分类" /> <asp:BoundField DataField="名称" HeaderText="名称" SortExpression="名称" /> <asp:BoundField DataField="作者" HeaderText="作者" SortExpression="作者" /> <asp:BoundField DataField="审核" HeaderText="审核" SortExpression="审核" /> <asp:BoundField DataField="备注" HeaderText="备注" SortExpression="备注" /> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:Button ID="Button4" runat="server" CommandName="Delete" Text="Button" /> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowDeleteButton="True" /> </Columns> <EditRowStyle BackColor="#2461BF" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#EFF3FB" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#F5F7FB" /> <SortedAscendingHeaderStyle BackColor="#6D95E1" /> <SortedDescendingCellStyle BackColor="#E9EBEF" /> <SortedDescendingHeaderStyle BackColor="#4870BE" /> </asp:GridView> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.OleDb; using System.Data.Common; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string id = e.Row.Cells[2].Text;//抓ID文字 string q = e.Row.Cells[5].Text;//抓名称文字 //抓CommandField的刪除Button if (e.Row.Cells[1].Controls.Count > 0) { Button deleteBtn = (Button)e.Row.Cells[1].Controls[0]; deleteBtn.Text = "删除:" + q; deleteBtn.OnClientClick = "if (confirm('你确认要删除:" + q + "?')) javascript:__doPostBack('GridView','Delete$" + e.Row.RowIndex.ToString() + "'); else return false;"; } //抓Button4 Button Button4 = (Button)e.Row.FindControl("Button4"); Button4.OnClientClick = "if (confirm('你确认要删除:" + q + "?')) javascript:__doPostBack('GridView','Delete$" + e.Row.RowIndex.ToString() + "'); else return false;"; } } }
经过多次测试 本代码还有缺陷 就是如果在GridView_RowDeleting中再填写代码会出现代码不被执行的问题。
绝不不懂装懂,不因为自己的问题低级而感到不好意思,踏踏实实的虚心学习。- 已标记为答案 沉默种子 2011年8月14日 13:22
全部回复
-
非常感谢 TerryChuang、Wei_Dong、Shadow And Happy Code 三位大师给予的帮助。
求助的问题1:还未解决,但是已经完成点击删除后提示的效果。解决代码如下:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string q = e.Row.Cells[1].Text;//抓ID文字 //抓CommandField的刪除Button Button btn = e.Row.Cells[0].Controls[2] as Button; btn.Text = "删除:" + q; btn.OnClientClick = "return confirm('你确认要删除:" + q + "?')"; } } //比较重要的地方是Button btn = e.Row.Cells[0].Controls[2] as Button; 其中重中之重是Controls[2],这里我曾经用Controls[0]修改的是gridview中commandfield中的编辑按钮,本以为改为Controls[1]就是删除按钮,但实际改后提示错误。但Controls[2]后运行正常。 至今没明白怎么回事。 //已经实现提示效果,但是点击确定后无法删除数据。另外,当单击“编辑”按钮后,对应的“取消”按钮也被提示,导致无法正常返回。
求助问题还没得到答案。等待中。希望这个帖子能给新手带来实例说明,免走弯路。
绝不不懂装懂,不因为自己的问题低级而感到不好意思,踏踏实实的虚心学习。- 已编辑 沉默种子 2011年8月14日 8:24
-
问题1 :是因為在該欄裡總共有三個Control
Button(编辑)、Literal(空白)、Button(删除)
所以才會用
e.Row.Cells[0].Controls[2]
问题2:我已经回帖在http://social.msdn.microsoft.com/Forums/zh-CN/295/thread/8acda820-83cb-4dfa-b306-114cca2ad5a4
Shadowと愉快なコード達 -
若要在非CommandFiled自訂Delete按鈕,可依照下列步驟進行:(以Button為例)
1. 設定Button的CommandName為Delete。
<asp:TemplateField> <ItemTemplate> <asp:Button ID="Button4" runat="server" CommandName="delete" onclick="Button4_Click" Text="Button" /> </ItemTemplate>
2.在GridView的RowDataBind事件加入刪除確認的提示訊息。protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string q = e.Row.Cells[1].Text; Button btn = e.Row.Cells[4].Controls[1] as Button; btn.Text = "删除:'" + q; btn.OnClientClick = "return confirm('你确认要删除:" + q + "?');"; } }
下圖為執行結果,請依照您的情境調整Cells的index。完整程式碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GV.aspx.cs" Inherits="WebApplication1.GV" %> <!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 runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="RegionID" DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound" > <Columns> <asp:CommandField ButtonType="Button" HeaderText="Delete" ShowDeleteButton="True" /> <asp:BoundField DataField="RegionID" HeaderText="RegionID" ReadOnly="True" SortExpression="RegionID" /> <asp:BoundField DataField="RegionDescription" HeaderText="RegionDescription" SortExpression="RegionDescription" /> <asp:BoundField DataField="RegionID" HeaderText="Delete" ReadOnly="True" SortExpression="RegionID" /> <asp:TemplateField> <ItemTemplate> <asp:Button ID="Button4" runat="server" CommandName="delete" onclick="Button4_Click" Text="Button" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT * FROM [Region]" DeleteCommand="DELETE FROM [Region] WHERE [RegionID] = @RegionID" InsertCommand="INSERT INTO [Region] ([RegionID], [RegionDescription]) VALUES (@RegionID, @RegionDescription)" UpdateCommand="UPDATE [Region] SET [RegionDescription] = @RegionDescription WHERE [RegionID] = @RegionID"> <DeleteParameters> <asp:Parameter Name="RegionID" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="RegionID" Type="Int32" /> <asp:Parameter Name="RegionDescription" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="RegionDescription" Type="String" /> <asp:Parameter Name="RegionID" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> </div> </form> </body> </html>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string q = e.Row.Cells[1].Text; Button btn = e.Row.Cells[4].Controls[1] as Button; btn.Text = "删除:'" + q; btn.OnClientClick = "return confirm('你确认要删除:" + q + "?');"; } }
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/ -
TerryChuang感谢您的帮助,在得到原生按钮答案后在标注为答案。谢谢您的无私帮助。
绝不不懂装懂,不因为自己的问题低级而感到不好意思,踏踏实实的虚心学习。 -
TerryChuang感谢您的帮助,在得到原生按钮答案后在标注为答案。谢谢您的无私帮助。
绝不不懂装懂,不因为自己的问题低级而感到不好意思,踏踏实实的虚心学习。
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/ -
关于由Shadow And Happy Code提供的在CommandField的刪除Button下已经实现的提示效果,但代码中存在的问题修正:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string q = e.Row.Cells[1].Text;//抓ID文字 //抓CommandField的刪除Button Button btn = e.Row.Cells[0].Controls[2] as Button; btn.Text = "删除:" + q; btn.OnClientClick = "return confirm('你确认要删除:" + q + "?')"; } } //已经实现提示效果,但是点击确定后无法删除数据。另外,当单击“编辑”按钮后,对应的“取消”按钮也被提示,导致无法正常返回。
代码无法删除数据行,在TerryChuang提供的代码帮助下修改代码:btn.OnClientClick = "return confirm('你确认要删除:" + q + "?')";为btn.OnClientClick = "if (confirm('你确认要删除:" + q + "?')) javascript:__doPostBack('GridView','Delete$" + e.Row.RowIndex.ToString() + "'); else return false;";
则可实现删除功能,希望有更简洁的代码出现。但是同样Button btn = e.Row.Cells[0].Controls[2] as Button;这行语句完成了提示功能,但是也导致了单击“编辑”按钮后,对应的“取消”按钮也被提示,导致无法正常返回的问题。鉴于个人知识和技术的匮乏,本人无法解决这个问题。尚希望高手能够给予更多的帮助。谢谢。
绝不不懂装懂,不因为自己的问题低级而感到不好意思,踏踏实实的虚心学习。 -
Shadow And Happy Code 提供原生delete按钮的源码
以下是根據您的需求重新寫過一遍的代碼
CommandField我拆成編輯、刪除兩欄了
<%@ Page Debug="true" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %> <!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 runat="server"> </head> <body> <form id="form1" runat="server"> <asp:SqlDataSource runat="server" ID="ObjectDataSourceshujubianji" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [shujubianji]" DeleteCommand="DELETE FROM [shujubianji] WHERE [ID] = @ID" InsertCommand="INSERT INTO [shujubianji] ([栏目], [分类], [名称], [作者], [审核], [备注]) VALUES (@栏目, @分类, @名称, @作者, @审核, @备注)" UpdateCommand="UPDATE [shujubianji] SET [栏目] = @栏目, [分类] = @分类, [名称] = @名称, [作者] = @作者, [审核] = @审核, [备注] = @备注 WHERE [ID] = @ID" > <DeleteParameters> <asp:Parameter Name="ID" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="栏目" Type="String" /> <asp:Parameter Name="分类" Type="String" /> <asp:Parameter Name="名称" Type="String" /> <asp:Parameter Name="作者" Type="String" /> <asp:Parameter Name="审核" Type="String" /> <asp:Parameter Name="备注" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="栏目" Type="String" /> <asp:Parameter Name="分类" Type="String" /> <asp:Parameter Name="名称" Type="String" /> <asp:Parameter Name="作者" Type="String" /> <asp:Parameter Name="审核" Type="String" /> <asp:Parameter Name="备注" Type="String" /> <asp:Parameter Name="ID" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> <asp:GridView ID="GridView" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" DataSourceID="ObjectDataSourceshujubianji" ForeColor="#333333" GridLines="None" OnRowDataBound ="GridView_RowDataBound" > <AlternatingRowStyle BackColor="White" /> <Columns> <asp:CommandField ShowEditButton="True" ButtonType="Button" HeaderText="編輯" /> <asp:CommandField ButtonType="Button" HeaderText="刪除" ShowDeleteButton="True" /> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="栏目" HeaderText="栏目" SortExpression="栏目" /> <asp:BoundField DataField="分类" HeaderText="分类" SortExpression="分类" /> <asp:BoundField DataField="名称" HeaderText="名称" SortExpression="名称" /> <asp:BoundField DataField="作者" HeaderText="作者" SortExpression="作者" /> <asp:BoundField DataField="审核" HeaderText="审核" SortExpression="审核" /> <asp:BoundField DataField="备注" HeaderText="备注" SortExpression="备注" /> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:Button ID="Button4" runat="server" CommandName="Delete" Text="Button" /> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowDeleteButton="True" /> </Columns> <EditRowStyle BackColor="#2461BF" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#EFF3FB" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#F5F7FB" /> <SortedAscendingHeaderStyle BackColor="#6D95E1" /> <SortedDescendingCellStyle BackColor="#E9EBEF" /> <SortedDescendingHeaderStyle BackColor="#4870BE" /> </asp:GridView> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.OleDb; using System.Data.Common; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string id = e.Row.Cells[2].Text;//抓ID文字 string q = e.Row.Cells[5].Text;//抓名称文字 //抓CommandField的刪除Button if (e.Row.Cells[1].Controls.Count > 0) { Button deleteBtn = (Button)e.Row.Cells[1].Controls[0]; deleteBtn.Text = "删除:" + q; deleteBtn.OnClientClick = "if (confirm('你确认要删除:" + q + "?')) javascript:__doPostBack('GridView','Delete$" + e.Row.RowIndex.ToString() + "'); else return false;"; } //抓Button4 Button Button4 = (Button)e.Row.FindControl("Button4"); Button4.OnClientClick = "if (confirm('你确认要删除:" + q + "?')) javascript:__doPostBack('GridView','Delete$" + e.Row.RowIndex.ToString() + "'); else return false;"; } } }
经过多次测试 本代码还有缺陷 就是如果在GridView_RowDeleting中再填写代码会出现代码不被执行的问题。
绝不不懂装懂,不因为自己的问题低级而感到不好意思,踏踏实实的虚心学习。- 已标记为答案 沉默种子 2011年8月14日 13:22