GridView自定义删除操作的具体方法

首先,我们前端的代码如下:

复制代码 代码如下:

                  DataKeyNames="P_ID" AutoGenerateColumns="False" 
                      RowStyle-HorizontalAlign="Center" BorderWidth="1px" PageSize="17"
                      onrowdeleting="gridViewDxjk_RowDeleting"
                      OnRowDataBound="gridViewDxjk_RowDataBound"
                      onpageindexchanging="gridViewDxjk_PageIndexChanging" >
                       
                       
                       
                       
                       
                       
                                                   
                        
                               

                               
                               
                               
                               

                               
                               
                               

                               
                               

                               
                               

                               
                               

                               
                               

                               
                               

                                                  
                               

                       

                       
                        没有数据!
                       

                        
                       
                           
                           
                           
                       

                                第
                                /共页  
                              首页
                              上一页
                              下一页
                              尾页
                             
                             
                           

                   

                


后端的话,由于需要弹出删除前的确认框,所以,我们需要在RowDataBound里面做点什么?同时,要想真正的删除,还需要触发RowDeleting事件,具体代码如下:

复制代码 代码如下:

//报警删除
        protected void gridViewDxjk_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string key = gridViewDxjk.DataKeys[e.RowIndex].Value.ToString();
            bool flag = bll.Delete(Int32.Parse(key));
            if (flag)
                NXT_WLService.App_Code.JScript.Alert("删除成功!", this);
            else
                NXT_WLService.App_Code.JScript.Alert("删除失败!", this);
        }

       
        protected void gridViewDxjk_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton btn = (LinkButton)e.Row.Cells[1].Controls[0];
                if (btn.Text.Equals("删除"))//�h除�o才加提示�息 
                    btn.OnClientClick = "if (confirm('你确认要删除?')) javascript:__doPostBack('gridViewDxjk','Delete$" + e.Row.RowIndex.ToString() + "'); else return false;";
            }
        }

你可能感兴趣的:(GridView自定义删除操作的具体方法)