Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 67,419 times

Contents

Related Categories

Object-Oriented ASP.NET - Real World Example

gregennis

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

  • Getting Error Message when running code

    Posted by ASP_Man on 31 Dec 2003

    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 y...

  • Posted by ElliotRodriguez on 06 Oct 2003

    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 refer...

  • Posted by kali_mist on 06 Oct 2003

    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
    Lo...

  • Getting it to work in VB.NET

    Posted by ElliotRodriguez on 03 Oct 2003

    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.DataGri...

  • Great Article

    Posted by ElliotRodriguez on 03 Oct 2003

    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!