Microsoft . 技术之路...

—— 专注于微软技术, 分享是快乐的源泉......
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

GridView 的刪除按钮增加确认提示

Posted on 2007-07-23 13:36  赣江源  阅读(421)  评论(0编辑  收藏  举报

一般在 GridView 的 Command 的删除按钮是沒有提示的确认信息。以下的范例就是要为GridView增加删除时的确认提示信息。
页面文件:*.aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Flag,ID"  
     DataSourceID
="SqlDataSource1" EmptyDataText="沒有记录。">   
     
<Columns>   
         
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />   
         
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />   
         
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />   
         
<asp:BoundField DataField="FaxNo" HeaderText="FaxNo" SortExpression="FaxNo" />   
         
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />   
         
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />   
    
</Columns>   
</asp:GridView>  

代码:*.aspx.vb
Partial Class _Default   
    
Inherits System.Web.UI.Page   
 
    
Function FindCommandButton(ByVal Control As Control, ByVal CommandName As StringAs Button   
        
Dim oChildCtrl As Control   
 
        
For Each oChildCtrl In Control.Controls   
            
If TypeOf (oChildCtrl) Is Button Then  
                
If String.Compare(CType(oChildCtrl, Button).CommandName, CommandName, True= 0 Then  
                    
Return oChildCtrl   
                
End If  
            
End If  
        
Next  
  
        
Return Nothing  
    
End Function  
  
    
Protected Sub GridView1_RowDataBound(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound   
        
Dim oButton As Button   
  
        
If e.Row.RowType = DataControlRowType.DataRow Then  
            
'找到该行的第一个Cell中的删除按钮,即 CommandField 中的删除按钮   
            oButton = FindCommandButton(e.Row.Cells(0), "Delete")   
            oButton.OnClientClick 
= "if (confirm('确定要删除该行吗?')==false) {return false;}"  
        
End If  
    
End Sub  
End Class