【转自http://www.cnblogs.com/doraeimo/archive/2007/01/01/609344.html

方法1:
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
         {
              foreach(TableCell tc in e.Item.Cells)//枚举该行中的每个单元格
              {
                   if(tc.Controls.Count>0)//检查每个单元格中的控件数
                   {
                       foreach(Control con in tc.Controls)//得到每个控件
                       {
                            if(con.ToString()=="System.Web.UI.WebControls.DataGridLinkButton")//检查每个控件,看是否是DataGridLinkButton
                                {
                                 LinkButton lb=(LinkButton)con;
                                 if(lb.CommandName=="Delete")
                                 {
                                     lb.Attributes.Add("onclick", "return confirm('您真的要删除此行吗?')");
                                 }
                            }
                       }
 
                   }
              }             
         }
方法2:
如图,在删除按钮的那一列属性里面,把DeleteText属性设为
给gridview添加确认对话框<div id="de" onclick="JavaScript:return confirm('确定删除吗?')">删除</div> 
给gridview添加确认对话框
可问题是,.net输出的html代码如下:
给gridview添加确认对话框<href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$GridView1','Delete$0')" style="color:#4A3C8C;"><div id="de" onclick="JavaScript:return confirm('确定删除吗?')">删除</div></a> 
为什么内层<div>的onclick事件所返回的bool值能影响到外层<a>标签的的语句是否被执行呢?
仔细想了想,以前用asp直接写的删除标签是这个样子的:
给gridview添加确认对话框<href="deleteUser.asp?id=xxx"  onclick="return confirm('确定删除吗?')">删除</a> 
也就是说,onclick事件接受一个bool值,其决定了click事件是否被触发。若click事件没有被触发,href内含的跳转动作就不会生效。同理,之前的那段代码中,<div>在<a>中,只有先触发<div>的click事件,<a>的跳转动作才会生效。而我们在<div>的onclick事件中若选择为其赋值false,随即取消了<div>的click事件,也同时取消了<a>的跳转。

之后,新的问题又来了。如果我们的CommandField中ButtonType是Button的话,这段代码就失效了。我想了下,可以通过将其转换为模板列的方式来解决。
先将该字段转换成模板,然后编辑这个模板列,选中用于删除的Button,将其onClientClick属性设为
给gridview添加确认对话框return confirm('您确认删除要删除么?')
即可。
给gridview添加确认对话框 

【转载】另一篇帖子
 

方法一:

VB代码

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
         If e.Row.RowType = DataControlRowType.DataRow Then
             e.Row.Cells(12).Attributes.Add("onclick", "return confirm('你确认要删除吗?')")
         End If
     End Sub

c#代码

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)
         {
             e.Row.Cells[5].Attributes.Add("onclick", "return confirm('你确认要编辑吗?')");

         }

}

方法二

在VS2005提供的GridView中我们可以直接添加一个CommandField删除列:<asp:CommandField ShowDeleteButton="True" />,然后在GridView的OnRowDeleting事件中完成删除。但一般情况下我们在做这种删除操作时都需要先让用户确认一下,然后后再删除记录,以避免误操作引起的误删除。

那我们可以通过下面方法给GridView删除前加上一个确认对话框。

首先,在GridView的属性对框话框中点击“Columns”进入它的"字段"设计器;或者在设计窗口直接点击GridView控件右上角的那个小箭头,点击"编辑列",进入"字段"设计器。

接着在"字段"设计器中的左下方"选定的字段"框中,选择以前已加上的那个CommandField“删除”列,这时在右边它的属性列表下会看到一个"将此它段转换为 TemplateFied"的项,点击将它转换为TemplateFied列。

然后退出"字段"设计器,切换到源码视图你会发现该列已由原来的:
<asp:CommandField ShowDeleteButton="True" />
变为了:
<asp:TemplateField ShowHeader="False">
                                 <ItemTemplate>
                                     <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"

CommandName="Delete"   Text="删除"></asp:LinkButton>
</ItemTemplate>

最后在<asp:LinkButton>中加入:OnClientClick="return confirm('您确认删除该记录吗?');"

这样点击删除时就会先在客户端弹出“您确认删除该记录吗?”对话框,点击"确定",则进行删除;点击"取消",则不删除.
而原来在onRowDeleting事件中写的代码完全不用改变。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-26
  • 2022-12-23
  • 2021-09-09
  • 2022-12-23
  • 2021-11-04
  • 2021-09-19
相关资源
相似解决方案