ASP.NET Today: Smart Delete

Published 06 July 05 11:14 AM | despos

DetailsView and FormView controls support delete operations and delegate the execution to the underlying data source control. If the data source control is configured to execute the delete operation, all works fine, otherwise an exception is thrown.

The DetailsView generates command buttons automatically and doesn't expose them directly to page code. How can you add a bit of Javascript code to ask for confirmation? Here's the code.

protected void DetailsView1_ItemCreated(object sender, EventArgs e)
{
  
// Test FooterRow to make sure all rows have been created 
 
  if (DetailsView1.FooterRow != null)
   {
     // The command bar is the last element in the Rows collection
    
int commandRowIndex = DetailsView1.Rows.Count-1;
    
DetailsViewRow commandRow = DetailsView1.Rows[commandRowIndex];
     
    
// Look for the DELETE button
    
DataControlFieldCell cell = (DataControlFieldCell) commandRow.Controls[0];
    
foreach(Control ctl in cell.Controls)
     {
      
LinkButton link = ctl as LinkButton;
      
if (link != null)
       {
          
if (link.CommandName == "Delete")
          {
             link.ToolTip =
"Click here to delete";
             link.OnClientClick =
"return confirm('Do you really want to delete this record?');";
          }
       }
    }
}

The ItemCreated event doesn't provide any information about the row being created. However, it can be figured out that the footer row is always created and is always the last row to be created. If the FooterRow object is not null, you can conclude that all rows have been created, including the command bar. The command bar is the first row after the data rows and is stored in the Rows collection--it's the last element in the collection. The command bar is a table row (type is DetailsViewRow) and contains a cell (type is DataControlFieldCell). The cell contains as many link buttons (type is DataControlLinkButton) as there are commands. Delete, Edit, New, Update, Cancel are the command names used and useful to identify the right button.


The FormView is fully templated and lets you manually define appearance and behavior of command buttons. If you place a custom button, or want to use a custom command name for a standard button (Edit, New, Delete), here's how to detect the click on the button. You start by adding a handler for ItemCommand. The code below shows how to deal with a custom Delete button that checks if the bound data source control is enabled for deletion before proceeding.

 protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
{
  if (e.CommandName == "SmartDelete")
  {
    IDataSource obj = (IDataSource) FindControl(FormView1.DataSourceID);
    DataSourceView view = obj.GetView("DefaultView");
    if (!view.CanDelete) {
       Response.Write(
"Sorry, you can't delete");
      
return;
    }
    else 
       FormView1.DeleteItem();
  }
}

Comments

# Ian Walker said on July 6, 2005 06:36 AM:

Things that make you go 'oooohh, nice bit of code'

Thanks!

# http://www.qiangzhe.cn said on July 14, 2005 12:41 PM:

贵站做的不错。打扰了,申请个连接!
[url=http://www.qiangzhe.cn]网络营销[/url]
[url=http://www.qiangzhe.cn/liuyan/]分类目录[/url]
[url=http://www.qiangzhe.cn/Html/wangluoyingxiao/]网络营销[/url]
[url=http://www.qiangzhe.cn/Html/wangluoyingxiaozhishi/]网络营销知识[/url]
[url=http://www.qiangzhe.cn/Html/wangluoyingxiaojiqiao/]网络营销技巧[/url]
[url=http://www.qiangzhe.cn/Html/wangluoyingxiaolunwen/]网络营销论文[/url]
[url=http://www.qiangzhe.cn/Html/wangluoyingxiaozhanlue/]网络营销战略[/url]
[url=http://www.qiangzhe.cn/Html/wangluoyingxiaoqianyan/]网络营销前沿[/url]
[url=http://www.qiangzhe.cn/Html/wangluoyingxiaocehua/]网络营销策划[/url]
[url=http://www.qiangzhe.cn/Html/wangluoyingxiaoanli/]网络营销案例[/url]
[url=http://wangluoyingxiao.blogbus.com/]网络营销[/url]
[url=http://wangluoyingxiao007.blogdriver.com/]网络营销[/url]
[url=http://wangluoyingxiao.yculblog.com/]网络营销[/url]
[url=http://wangluoyingxiao1.blogbus.com/]网络营销[/url]
[url=http://hainan35.blogchina.com/]网络营销[/url]
[url=http://wangluoyingxiao.blogdriver.com/]网络营销[/url]
[url=http://www.0898it.net/]海南旅游[/url]

# Brad said on August 2, 2005 03:25 PM:

Doesn't the ItemCreated event get fired once for every row in the DetailsView? Why not place the same code in a handler for the DataBound event so it only has to run once?

# DinoE said on August 3, 2005 05:44 AM:

Hi Brad,
DataBound (more exactly RowDataBound for GridViews) is fired as many times as RowCreated. The two go one after the next with the significant fact that RowDataBound is not fired for non-databound items like hte command bar or pager bar.

# Brad said on August 3, 2005 01:50 PM:

Oh, very good. I see now there is no GridView.DataBound event. Only RowDataBound. My mistake.

# Louie said on July 20, 2007 08:50 AM:

Awesome solutions!

# Mark said on October 26, 2007 11:44 AM:

I added this code to the page and I don't get prompted to delete. Is there anything else I need to add to the page to initiate the popup before delete?

# Venkatanarasimha KL said on November 19, 2007 03:56 AM:

Hi I am getting below mentioned Error;

System.ArgumentOutOfRangeException was unhandled by user code

 Message="Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"

How do I overcome this please?

Venkat.KL

Leave a Comment

(required) 
(required) 
(optional)
(required)