DataGrid,GridView和DetailsView中添加删除确认提示

http://lei123.cnblogs.com/archive/2006/03/21/dataitemdelete.html

 

本文主要写如何在dotnet的DataGrid(1.0),GridView(2.0)和DetailsView(2.0)中添加删除确认提示。

先说1.0的DataGrid,添加DataGrid的ItemDataBound事件并添加以下类似代码

< asp:TemplateColumn HeaderText = " 删除 " >
                                                
< ItemTemplate >
                                                    
< asp:ImageButton ID = " delete "  Runat = " server "  CommandName = " DelInfo "  ImageAlign = " Middle "  ImageUrl = " Images/btn_del.gif " ></ asp:ImageButton >
                                                
</ ItemTemplate >
  
</ asp:TemplateColumn >

 


private   void  DataGrid_ItemDataBound( object  sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        
{
            
if  (e.Item.ItemType == ListItemType.Item  ||  e.Item.ItemType == ListItemType.AlternatingItem)
            
{
                DataRowView drv
= (DataRowView)e.Item.DataItem;
            
                ImageButton l
= (ImageButton)e.Item.Cells[ 4 ].FindControl( " delete " );
                l.Attributes.Add(
" onclick " , " javascript:return confirm('你确认要删除 " + drv.Row[ " SName " ].ToString() + " 吗?') " );
            }

        }


注意:" SName "确认中要添加确认数据的列名,此例中用的是ImageButton其它按钮与此类似。

再说2.0的GridView,添加GridView的RowDataBound事件并添加以下类似代码

< asp:CommandField ShowDeleteButton = " True "   />

  protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
    
{
        
if  (e.Row.RowType  ==  DataControlRowType.DataRow)
            e.Row.Cells[
10 ].Attributes.Add( " onclick " " javascript:return confirm('你确认要删除/ ""  + e.Row.Cells[1].Text +  " / " 吗?') " );

    }

注意:Cells[10]为删除按钮所在的列,删除按钮要在一个单独的列中,可以转化为模板列。

最后说DetailsView,直接利用.Net控件的OnClientClick如下:
< asp:TemplateField ShowHeader = " False " >
                    
< ItemTemplate >
                        
< asp:LinkButton ID = " LinkButton1 "  runat = " server "  OnClientClick = "  javascript:return confirm('你确认要删除吗?') "  CausesValidation = " False "  CommandName = " Delete "
                            Text
= " 删除 " ></ asp:LinkButton >
                    
</ ItemTemplate >
                
</ asp:TemplateField >


好啦,到此结束。

 

 

 

 

 

Feedback

#1楼  回复  引用  查看    

2006-03-21 14:11 by dudu        
文章应该放在新手区比较合适,希望不要再移回首页了。

#2楼  回复  引用    

2006-03-21 14:12 by sss[未注册用户]
虽然老早就知道了,但是还是要谢谢。顺手问一句,Gridview采用双主键的表做数据源,怎么完成删除,更改啊?总是不行,很麻烦

#3楼 [楼主 ]  回复  引用  查看    

2006-03-21 20:17 by 磊        
to dudu 知道了

#4楼 [楼主 ]  回复  引用  查看    

2006-03-22 07:27 by 磊        
to sss 双主键的表做数据源是可以的,你看一下下面的代码(用的是Northwind数据库)


< asp:GridView ID = " GridView1 "  runat = " server "  AutoGenerateColumns = " False "  DataKeyNames = " EmployeeID,TerritoryID "
                DataSourceID
= " SqlDataSource1 "  AllowPaging = " True " >
                
< Columns >
                    
< asp:BoundField DataField = " EmployeeID "  HeaderText = " EmployeeID "  ReadOnly = " True "  SortExpression = " EmployeeID "   />
                    
< asp:BoundField DataField = " TerritoryID "  HeaderText = " TerritoryID "  ReadOnly = " True "
                        SortExpression
= " TerritoryID "   />
                    
< asp:CommandField ShowDeleteButton = " True "   />
                
</ Columns >
            
</ asp:GridView >
            
< asp:SqlDataSource ID = " SqlDataSource1 "  runat = " server "  ConnectionString = " Data Source=(local);Initial Catalog=Northwind;Integrated Security=True "
                DeleteCommand
= " DELETE FROM [EmployeeTerritories] WHERE [EmployeeID] = @EmployeeID AND [TerritoryID] = @TerritoryID "
                InsertCommand
= " INSERT INTO [EmployeeTerritories] ([EmployeeID], [TerritoryID]) VALUES (@EmployeeID, @TerritoryID) "
                ProviderName
= " System.Data.SqlClient "  SelectCommand = " SELECT [EmployeeID], [TerritoryID] FROM [EmployeeTerritories] " >
                
< DeleteParameters >
                    
< asp:Parameter Name = " EmployeeID "  Type = " Int32 "   />
                    
< asp:Parameter Name = " TerritoryID "  Type = " String "   />
                
</ DeleteParameters >
                
< InsertParameters >
                    
< asp:Parameter Name = " EmployeeID "  Type = " Int32 "   />
                    
< asp:Parameter Name = " TerritoryID "  Type = " String "   />
                
</ InsertParameters >
            
</ asp:SqlDataSource >


#5楼  回复  引用    

2006-04-02 16:20 by jingelcat[未注册用户]
在模板列获取子控件引用一般不使用cells集合索引:e.Item.Cells[4].FindControl("delete");
而直接使用模板项的FindControl方法:e.Item.FindControl("delete");
因为添加或者删除列每个列的索引可能变化了,需要一个个手动的修改

#6楼  回复  引用    

2006-04-03 10:34 by 大恐龍[未注册用户]
detailview 用
<asp:CommandField CancelText="取消" DeleteText="刪除" EditText="修改" InsertVisible="False" ShowDeleteButton="True" ShowEditButton="True" UpdateText="更新" />
的內建 command button,
有辦法做到刪除提示嗎 ?

#7楼 [楼主 ]  回复  引用  查看    

2006-04-04 15:00 by 磊        
@jingelcat
是好多了,谢谢

#8楼 [楼主 ]  回复  引用  查看    

2006-04-04 15:07 by 磊        
@大恐龍
可以但很麻烦并且也不好用,还是转换为模板列吧。

#9楼  回复  引用    

2006-07-03 17:08 by zzk526[未注册用户]
@大恐龍
可以啊

你可以用DetailsView_ItemDeleting
做一个判断就可以了
如果
e.Cancel = True

那么就是取消删除!

#10楼  回复  引用    

2006-11-29 11:30 by Fallingstar[未注册用户]
2.0的GridView,添加GridView的RowDataBound事件并添加以下类似代码
根本不正确,提示是提示了,但是取消的时候还是会继续删除!

#11楼  回复  引用    

2007-04-10 16:03 by 哈哈[未注册用户]
是啊。。添加GridView的RowDataBound事件必须用在button类型为linkbutton的按钮上,用在image类型上就不行,出现向上面那位兄弟说道额情况一样郁闷啊!有没有好的办法

#12楼  回复  引用    

2007-05-17 11:29 by jell[未注册用户]
@大恐龍
可以,脚本改如下
当只有取消时才return;

Attributes.Add("onclick", "javascript:if (!confirm('你确认要删除/"" + e.Row.Cells[0].Text + "/"吗?'))return;");

你可能感兴趣的:(DataGrid,GridView和DetailsView中添加删除确认提示)