当前位置:首页 > 博客园 >对带有Checkbox的DataGrid数据删除确认操作

对带有Checkbox的DataGrid数据删除确认操作

  作者:Tim工作室   来源: 博客园-timsoft  
有2种方法:一种是在客户端判断,另一是在服务端判断

在客户端判断
(1)脚本
             < asp:DataGrid  id ="DataGrid1"  runat ="server"  AutoGenerateColumns ="False"  Width ="472px" >
                
< Columns >
                    
< asp:TemplateColumn >
                        
< ItemTemplate >
                            
< asp:CheckBox  id ="Checkbox2"  runat ="server" ></ asp:CheckBox >
                        
</ ItemTemplate >
                    
</ asp:TemplateColumn >
                    
< asp:BoundColumn  DataField ="LastName"  HeaderText ="LastName" ></ asp:BoundColumn >
                    
< asp:BoundColumn  DataField ="FirstName"  HeaderText ="FirstName" ></ asp:BoundColumn >
                
</ Columns >
            
</ asp:DataGrid >
            
< asp:Button  id ="Button1"  runat ="server"  Text ="Button" ></ asp:Button >

(2)JavaScript
 
         < script language = " javascript " >
        
function  SelCount()
        
{
            
var sum = 0;
            
var RowCount = <%=RowCount%>;//document.all.DataGrid1.rows.length;
            for(j= 1;j<RowCount+1;j ++ )
            
{
                
if(typeof(document.all.DataGrid1.rows(j).cells(0).children(0)) == "object")
                
{
                    
if( document.all.DataGrid1.rows(j).cells(0).children(0).checked == true )
                    
{
                     sum 
= sum +1;
                    }
            
                }

            }

            
if( sum >0)
            
{
                
return window.confirm("确定删除么?");
            }

            
else
            
{
                
var message = "请选择要删除的数据!";
                window.alert( message );
                
return false;
            }

        }

        
</ script >

(3)代码
        public int RowCount;
            
private   void  Page_Load( object  sender, System.EventArgs e)
        
{
            
//页面初试化时进行数据绑定
            if(!IsPostBack)
            
{
                DataGridDataBind();
            }

            Button1.Attributes.Add(
"onclick","return SelCount();");
        }


        
private   void  Button1_Click( object  sender, System.EventArgs e)
        
{
            
//执行删除操作
            DataGridDataBind();
        }
DataGridDataBind是数据绑定方法,在这个方法中要RowCount 赋行数值

在服务端判断
(1)脚本
             < asp:DataGrid  id ="DataGrid1"  runat ="server"  AutoGenerateColumns ="False"  Width ="472px" >
                
< Columns >
                    
< asp:TemplateColumn >
                        
< ItemTemplate >
                            
< asp:CheckBox  id ="Checkbox2"  runat ="server" ></ asp:CheckBox >
                        
</ ItemTemplate >
                    
</ asp:TemplateColumn >
                    
< asp:BoundColumn  DataField ="LastName"  HeaderText ="LastName" ></ asp:BoundColumn >
                    
< asp:BoundColumn  DataField ="FirstName"  HeaderText ="FirstName" ></ asp:BoundColumn >
                
</ Columns >
            
</ asp:DataGrid >
            
< asp:Button  id ="Button1"  runat ="server"  Text ="Button" ></ asp:Button >
            
< input  type ="button"  id ="btnRemove"  runat ="server"  value ="Delete"  style ="" >
(2)JavaScript
< script language = " javascript " >
            
function  DeleteItems(msg)
            
{
                
if(!confirm(msg))
                
{
                    
return;
                }

                
else
                
{
                    __doPostBack('btnRemove','');                
                }

            }

        
</ script >
(3)代码
         private   void  Page_Load( object  sender, System.EventArgs e)
        
{
            
//页面初试化时进行数据绑定
            if(!IsPostBack)
            
{
                DataGridDataBind();
            }

        }


        
private   void  Button1_Click( object  sender, System.EventArgs e)
        
{
            
//判断是否有选中 如果有选中的数据
            Page.RegisterStartupScript( "Delete""<script>DeleteItems('你确认要删除么?');</script>" ); 
        }


        
private   void  btnRemove_ServerClick( object  sender, System.EventArgs e)
        
{
            
//执行删除操作
            DataGridDataBind();
        }

相关阅读:

来源:(微信/QQ:420434200,微信公众号:makaidong-com)
       欢迎分享本文,转载请保留出处!

       【原文阅读】:http://www.cnblogs.com/timsoft/articles/414822.html
没有了

  • 评论:
  • 赞助本站

发表一针见血的评论,你还等什么? ◎欢迎参与讨论,我的微信公众号是:makaidong-com
马开东博客赞助