Click here to Skip to main content
15,893,668 members
Articles / Web Development / ASP.NET

Object-Oriented ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.22/5 (26 votes)
10 Aug 20035 min read 185.6K   1K   58  
Use the powerful object-oriented features of C# and VB.NET to build re-usable classes in ASP.NET
using System;
using System.Collections;
using System.Web;
using System.Web.UI.WebControls;

namespace ooaspnet
{
	/// <summary>
	/// 
	/// </summary>
	public class ConfirmDelDataGrid : System.Web.UI.WebControls.DataGrid
	{
		// Index of the "Delete" command column for this grid
		protected int delcol = -1;
		protected bool confirmdel = true;

		// Property to enable/disable deletion confirmation
		public bool ConfirmOnDelete 
		{ 
			get { return confirmdel; } 
			set { confirmdel = value; } 
		}

		public ConfirmDelDataGrid()
		{
		}

		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;
		}

		protected override void OnItemDataBound(DataGridItemEventArgs e)
		{
			// Bind it first
			base.OnItemDataBound (e);

			// Confirm delete enabled?
			if (!confirmdel) return;

			// Attach javascript confirmation to the delete button
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
			{
				if (delcol != -1)
				{
					e.Item.Cells[delcol].Attributes.Add("onclick", "return confirm('Are you sure?')");
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions