为Gridview的CommandField删除添加 "是否确认删除?"
开始的时候都是这样处理的:
 
 protected void grvShow_RowDataBound(object sender, GridViewRowEventArgs e)
protected void grvShow_RowDataBound(object sender, GridViewRowEventArgs e) {
        {
 
            if (e.Row.RowIndex != -1)                     //绑定javascript脚本,弹出提示框,是否确认删除数据!!
            if (e.Row.RowIndex != -1)                     //绑定javascript脚本,弹出提示框,是否确认删除数据!! {
            { e.Row.Cells[17].Attributes.Add("onclick", "javascript:return confirm('是否确认删除?')" );
                e.Row.Cells[17].Attributes.Add("onclick", "javascript:return confirm('是否确认删除?')" ); }
            } }
       }

当点删除的时候,也确实发现可以,但是同时也发现,不止点删除会提示,点这个(cell )的空白处也会弹出提示框.
觉得不太合理.于是打开javascript源码,发现其实删除就是一个
<td><a href="javascript:__doPostBack('ctl00$cphView$grvShow','Delete$8')" style="color:#333333;">删除</a></td>
如果要达到我们效果,就是只有当点删除的时候才弹出确认框,那么也就是只有当点<A>标记是才执行.
于是就考虑到<a onclick="javascript:return confirm('是否确认删除?')" ....../>.就可以达到效果了.
改进以后的做法:
后台处理:
 
 protected void grvShow_RowDataBound(object sender, GridViewRowEventArgs e)
protected void grvShow_RowDataBound(object sender, GridViewRowEventArgs e) {
        {
 
            if (e.Row.RowIndex != -1)                     //绑定javascript脚本,弹出提示框,是否确认删除数据!!
            if (e.Row.RowIndex != -1)                     //绑定javascript脚本,弹出提示框,是否确认删除数据!! {
            { e.Row.Cells[17].Attributes.Add("onmousemove", "javascript:show('" + e.Row.Cells[17].ClientID + "')");
                e.Row.Cells[17].Attributes.Add("onmousemove", "javascript:show('" + e.Row.Cells[17].ClientID + "')"); }
            } }
}

前台脚本:
//为前台的Gridview的删除添加一个 确认信息
 
 function show(id) //id  为当前<td>的ID
function show(id) //id  为当前<td>的ID {
 { 
   var c=window.document.getElementById(id).innerHTML;//获取<td></td>之间的脚本元素
   var c=window.document.getElementById(id).innerHTML;//获取<td></td>之间的脚本元素 var d=c.split('<A');
   var d=c.split('<A'); if(d[1].indexOf('onclick')==-1)
     if(d[1].indexOf('onclick')==-1) {
      { var J="<A onclick=\"javascript:return confirm('是否确定删除!')\" "+d[1];
       var J="<A onclick=\"javascript:return confirm('是否确定删除!')\" "+d[1];   window.document.getElementById(id).innerHTML=J;          //加上一个onclick事件
       window.document.getElementById(id).innerHTML=J;          //加上一个onclick事件 }
      } 
   }
 }
 最后测试了一下可以通过,但并不确信这个就是最好的方法,如果有更好的方法,请大家贴上.......学习交流!
 
                     
                    
                 
                    
                
 
 
        

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号