Library tutorials & articles

Object-Oriented ASP.NET

Real World Example

For this example I will be using a common scenario developers encounter with the DataGrid. The DataGrid gives us some powerful pre-built column objects, including command columns which allow the user to press buttons (or links) to select, edit, or delete items in the grid. Imagine you are writing a web form which uses a DataGrid having a "Delete" column, and you want to make the user confirm the deletion before actually deleting the item.

This is accomplished by using some client-side javascript to invoke the confirm function in the onclick handler:

<... onclick="return confirm('Are you sure?');" >

The way that you attach this code to the Delete button is to add to the item's Attributes collection when the item is created:

Control.Attributes.Add("onclick", "return confirm('Are you sure?')");

You will typically find code like this in the Page class which is handling the OnItemCreated event of the grid. It usually goes something like this:

private void InitializeComponent()
{
    this.grid.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.OnItemCreated);
    // ...(other handlers)...
}

private void OnItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Button btn = e.Item.FindControl("delbutton");
        btn.Attributes.Add("onclick", "return confirm('Are you sure?')");
    }
}

I find this approach to be flawed for two reasons:

  1. Encapsulation is being violated. Why is it the job for the Page object to fiddle around with the internal attributes of the datagrid? It would be ideal if the page object could simply set a boolean property to enable the confirmation message box.
  2. If you want to have delete confirmations for all 5 grids in all 5 pages of your application, you are going to have to repeat at least some of this code 5 times in your page classes. You can try to work around this by sharing some common code, but you still have to hook up an event handler for each grid in each page's InitializeComponent.

Comments

  1. 31 Dec 2003 at 13:13

    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?')");
               }
           }
           
       }
    }

  2. 06 Oct 2003 at 07:26

    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?

  3. 06 Oct 2003 at 06:10

    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

  4. 03 Oct 2003 at 13:54

    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


  5. 03 Oct 2003 at 13:51

    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!

  6. 03 Oct 2003 at 11:20

    I'm using VB.NET 2003 and I don't see the "Add" option when I right click in the Class view either.

  7. 30 Sep 2003 at 23:13

    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?

  8. 01 Jan 1999 at 00:00

    This thread is for discussions of Object-Oriented ASP.NET.

Leave a comment

Sign in or Join us (it's free).

Greg Ennis
AddThis

Related podcasts

We'd love to hear what you think! Submit ideas or give us feedback