Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Friday, November 9, 2007

.NET Smart Delete (Details View Client-side delete confirmation)

I was working on a database-driven web site and I needed a way to make sure that the admin couldn't delete all the admin user accounts (that was simple enough), but I also wanted a javascript confirmation popup when an admin did delete accounts.

I found exactly what I was looking for on Dino Esposito's WebLog on his post ASP.NET Today: Smart Delete:

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");

No comments:

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.