注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

伯纳乌の夢

可以挽回么?我们按“ Ctrl+Z”撤销掉吧。对不起啦~~

 
 
 

日志

 
 
 
 

GridView RowDeleting 删除行记录 不删除数据库中记录  

2011-05-08 23:38:58|  分类: Web |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
[Trackback]http://www.wherecanibuyit.co.uk/ASP/RowDeleting.html

This article describes how I implemented a GridView in ASP.NET that only allows you to delete rows from it that meet certain criteria. If they don't meet the criteria, an error message pops up.

The special delete buttons in the GridView

I had a GridView, and for each row I'd set it up to have edit and delete buttons like this:

GridView RowDeleting 删除行记录 不删除数据库中记录 - mgracy - 伯纳乌の夢
I now wanted to make it so that the delete button didn't simply delete the row, but first looked in the database to see if it is ok to delete the row, and cancel the delete operation if it is not ok. I mean, you can't just delete a product from your database if it features on one of your client's orders, can you? You've got to look in the database, check it isn't going to ruin your data integrity, then do the delete if things are ok.

Also, I wanted it to popup a warning box to say "Sorry, you can't delete that item as it is used by this record: xyz". The only way I could get it to do that was to use JavaScript.

How to implement the GridView

Here is a two-point sumary of what I did:

  1. I made it so that when the GridView is constructed, each Delete button is customized. As each row of the GridView is built, a database lookup is done to see if the row is deletable or not. If not, the Delete button gets its onclick event set to trigger a Javascript alert box saying why it can't be deleted.
  2. Then when a Delete button is clicked, I used the RowDeleting event, look at the Delete button for this row, and see if it has an onclick event set up for it. If not, fine, let ASP.NET delete the row. If it has, block the delete and let the JavaScript alert box fire off.

This is what you need to do:

  1. First, create an event handler for the GridView RowDataBound event. We are going to modify the Delete button in each row as it is databound. This way we can link a javascript alert popup to the delete button in each row, if the row can't be deleted.
  2. In RowDataBound, make sure that we are actually doing a data row here, not a heading, by using:
    if (e.Row.RowType == DataControlRowType.DataRow)
  3. Find the Delete button for this row using something like:
    Button delete = (Button)e.Row.Cells[0].Controls[2];
  4. Do an SQL query to test if we can delete this row or not. I got hold of the row id by using:
    string part_id = e.Row.Cells[1].Text;
  5. If we find that we can't delete the row, add the onclick event like this:
    delete.Attributes.Add("onclick", "alert('You cannot delete this row')");
  6. Now link up the RowDeleting event for the GridView. When a RowDeleting event is called, we can find the Delete button in the row by using:
    Button delete = (Button)GridView2.Rows[e.RowIndex].Cells[0].Controls[2];
  7. Then, if we find that the delete button has an onclick event, just set e.Cancel=true; The parameter e is passed with the RowDeleting event, and setting e.Cancel=true stops the row from being deleted. You can check if the onclick attribute is set by using something like:
    string isitset=delete.Attributes[("onclick")];
    The onclick event will fire and the alert box will appear.
    If we don't set e.cancel = true, then e.cancel remains false, and the row gets deleted for us by the inner workings of ASP.NET.

And that is pretty much it.

如果只需删除GridView中的行, 并不删除数据库中的记录

项次工号姓名 
110022936gracy.ma删除
210017300eric.mak删除

 protected void Button1_Click(object sender, EventArgs e)
        {
            int rowCount = 1;
            DataTable DT = new DataTable();
            DataRow DR;
            DT.Columns.Add("ORDER_NO");
            DT.Columns.Add("EMP_NO");
            DT.Columns.Add("NAME");
            for (int iRow = 0; iRow < GridView1.Rows.Count; iRow++)
            {
                 DR = DT.NewRow();
                DR[0] = rowCount;
                DR[1] = GridView1.Rows[iRow].Cells[1].Text.Trim();
                DR[2] = GridView1.Rows[iRow].Cells[2].Text.Trim();
                DT.Rows.Add(DR);
                rowCount++;
            }

             DR = DT.NewRow();
            DR[0] = rowCount;
            DR[1] = this.TextBox1.Text.Trim();
            DR[2] = this.TextBox2.Text.Trim();
            DT.Rows.Add(DR);
            GridView1.DataSource = DT;
            GridView1.DataBind();
            Session["DataTable"] = DT;
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataTable DT=(DataTable)  Session["DataTable"];
            DT.Rows.RemoveAt(e.RowIndex);
            GridView1.DataSource = DT;
            GridView1.DataBind();

        }

  评论这张
 
阅读(1123)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017