Library tutorials & articles
Object-Oriented ASP.NET
- Introduction
- Real World Example
- A Better Way
- Putting it All Together
A Better Way
We will derive a new class from DataGrid, called ConfirmDelDataGrid. The derived class will internally handle the code to attach the javascript attributes. In order to take advantage of deletion confirmation, the page class merely has to create an instance of ConfirmDelDataGrid and do nothing else.
Creating a Derived DataGrid
To create the new customized DataGrid, right click on your project in the Class View and select "Add... Class". You must be in the class view! If you try this in the solution explorer, you can only add generic classes to your project. Type a class name and then switch to the Base Class panel. Select the DataGrid as your base class, like this:
Once you have added your new derived class, we will go about customizing the
behavior. First we need a way to determine which column is the Delete column
in the grid. One approach is to override the DataGrid's CreateColumnSet method
and examine the columns as they are created:
// Class member that stores
the index of the "Delete" column of this grid
protected int delcol = -1;
protected override ArrayList CreateColumnSet(PagedDataSource
dataSource, bool useDataSource)
{
// Let the DataGrid create the columns
ArrayList list = base.CreateColumnSet (dataSource, useDataSource);
// Examine
the columns
delcol=0;
foreach (DataGridColumn col in list)
{
// If this column is the "Delete" button command column...
if ((col is ButtonColumn) && (((ButtonColumn)col).CommandName == "Delete"))
{
// Found it
break;
}
delcol++;
}
// If we did not find a delete column, invalidate the index
if (delcol == list.Count) delcol = -1;
// Done
return list;
}
By overriding the CreateColumnSet method we get a chance to examine the columns
right after they are created by the DataGrid base class. We figure out which
column (if any) is the "Delete" column and store that index in a
protected member variable.
Next we will override the OnItemDataBound method of the DataGrid base class.
First we let the DataGrid bind the item, then we attach the javascript to
the delete column (if any):
// Property to enable/disable deletion confirmation
protected bool confirmdel = true;
public bool ConfirmOnDelete
{
get { return confirmdel; }
set { confirmdel = value; }
}
protected override void OnItemDataBound(DataGridItemEventArgs e)
{
// Create it first in the DataGrid
base.OnItemDataBound (e);
// Attach javascript confirmation to the delete button
if ((confirmdel) && (delcol != -1))
{
e.Item.Cells[delcol].Attributes.Add("onclick", "return confirm('Are
you sure?')");
}
}
So that is the code internal to the DataGrid. Notice that the new behavior is built-in to the class and a Page object who uses the class does not need to do anything to enable the deletion confirmation message box.
Related articles
Related discussion
-
Very Urgent regarding deleting the images from a folder
by Nanosteps (6 replies)
-
New style of Javascript used in extenders.
by mittalpa (0 replies)
-
roulette game in windows application
by pdhanik (1 replies)
-
Chart insertation in a windows form...
by pdhanik (1 replies)
-
Bookstore in trieste
by atmir04041987 (1 replies)
Related podcasts
-
StackOverflow uses ASP.NET MVC - Jeff Atwood and his technical team
Scott chats with Jeff Atwood of CodingHorror.com and most recently, StackOverflow.com. Jeff and Joel Spolsky and their technical team have created a new class of application using ASP.NET MVC. What works, what doesn't, and how did it all go down?
Events coming up
-
Mar
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
I am getting as error message of
C:\Program Files\ASP.NET Starter Kits\ASP.NET Portal (CSSDK)\PortalCSSDK\RegDataGrid.cs(20): The type or namespace name 'DataSource' could not be found (are you missing a using directive or an assembly reference?)
and
C:\Program Files\ASP.NET Starter Kits\ASP.NET Portal (CSSDK)\PortalCSSDK\RegDataGrid.cs(54): The type or namespace name 'DataGridItemEventArgs' could not be found (are you missing a using directive or an assembly reference?)
when running the code. What am I missing?
Here is the code I am running.
using System;
namespace ASPNET.StarterKit.Portal
{
/// <summary>
///
/// </summary>
public class RegDataGrid : System.Web.UI.WebControls.DataGrid
{
public RegDataGrid()
{
//
// TODO: Add constructor logic here
//
}
// Class member that stores the index of the "Delete" column of this grid
protected int delcol = -1;
protected override ArrayList CreateColumnSet(PagedDataSource dataSource, bool useDataSource)
{
// Let the DataGrid create the columns
ArrayList list = base.CreateColumnSet (dataSource, useDataSource);
// Examine the columns
delcol=0;
foreach (DataGridColumn col in list)
{
// If this column is the "Delete" button command column...
if ((col is ButtonColumn) && (((ButtonColumn)col).CommandName == "Delete"))
{
// Found it
break;
}
delcol++;
}
// If we did not find a delete column, invalidate the index
if (delcol == list.Count) delcol = -1;
// Done
return list;
}
// Property to enable/disable deletion confirmation
protected bool confirmdel = true;
public bool ConfirmOnDelete
{
get { return confirmdel; }
set { confirmdel = value; }
}
protected override void OnItemDataBound(DataGridItemEventArgs e)
{
// Create it first in the DataGrid
base.OnItemDataBound (e);
// Attach javascript confirmation to the delete button
if ((confirmdel) && (delcol != -1))
{
e.Item.Cells[delcol].Attributes.Add("onclick", "return confirm('Are you sure?')");
}
}
}
}
Mick:
shoot me an email at elliotmrodriguezathotmaildotcom and I will send you the class file. Perhaps we can work together on this.
Oddly enough, when I compile (without errors) and set a reference to the file in my web page, the grid never gets displayed. I've stepped through the code on the webpage that sets its properties, and they work fine; Intellisense shows my properties for the class, so I know its being seen, but none of the events in the class itself get called. I am perplexed.
Greg Ennis, can you also help?
Hi Elliot,
Is there any chance that you can show me the code for the derived datagrid in VB.net.
Im having trouble creating it myself. Im new to this subclassing stuff.
Thank you
Mick Lennon
Louth
Ireland
You have to add the class manually as a generic class, then use the Inherits statement
Public Class DeleteDataGrid ' or whatever you want to call it
Inherits System.Web.UI.WebControls.DataGrid
Hitting ENTER after the Inherits statement will make the stubs appear automagically for both CreateControlHierarchy and PrepareControlHierarchy. OnItemDataBound, however, does not show up; you will have to add the function signature yourself.
HTH
great article. I was able to implement the grid in VB.NET quite easily, and I plan on extending it in similar fashion to create DataGrids with selectable rows. Thanks Greg!
I'm using VB.NET 2003 and I don't see the "Add" option when I right click in the Class view either.
Great article, but am I missing something. I go into Class view, but I get no "Add" option when I right-click on the project. This is an ASP.Net (VB.Net) project, so is it the case that this is not available in ASP.Net projects?
This thread is for discussions of Object-Oriented ASP.NET.