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

Three Tier Code Generator For ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.78/5 (34 votes)
8 Jul 200512 min read 426.3K   22.2K   251  
Generates three tier code for ASP.NET.
//**==========================================================================================
//**
//** FILE  : Default.aspx.cs
//** Author: Stevan Rodrigues
//**
//**==========================================================================================
//**
//** (c) The contents of this file , and of any file or document derived from it , are copyright 
//** to Webchamps . Unlicensed alteration, change or copying in any form, 
//** whether written, by photocopy, by print or by any other methods of reproduction is 
//** strictly prohibited.
//**
//**==========================================================================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using WebChamps.Components.Web;
using  Company.ThreeTierDemo.Components.Business;

//*********************************************************************
//
// Company.ThreeTierDemo.Admin Namespace
//
//*********************************************************************
namespace Company.ThreeTierDemo.Admin
{
	public class DefaultAdmin : WebChamps.Components.Web.SitePage
	{
		protected System.Web.UI.WebControls.Label PageMessage;
		protected System.Web.UI.WebControls.Label ErrorMessage;

		protected System.Web.UI.WebControls.DataGrid MsDataGrid;
		protected System.Web.UI.WebControls.HyperLink lnkAdmin;

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.MsDataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.MsDataGrid_ItemDataBound);
			this.MsDataGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.MsDataGrid_PageIndexChanged);
			this.MsDataGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MsDataGrid_CancelCommand);
			this.MsDataGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MsDataGrid_EditCommand);
			this.MsDataGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MsDataGrid_UpdateCommand);
			this.MsDataGrid.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.MsDataGrid_SortCommand);
			this.MsDataGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MsDataGrid_DeleteCommand);

		}
		#endregion
		//*********************************************************************
		//
		// Page_Load event
		//
 		//The ViewState["SortExpression"] used to sort the datagrid is initialised.
 		//The datagrid is then populated using the BindDataGrid function.
		//
		//*********************************************************************
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			// Insert code to process code when the page is loaded for first time or without a post back.
			if ( ! IsPostBack)
			{
				if (ViewState["SortExpression"] != null)
				{
					ViewState["SortExpression"] = "AdminId";
				}
				BindDataGrid(Convert.ToString(ViewState["SortExpression"]));
			}
		}

		//*********************************************************************
		//
		// BindDataGrid method
		//
 		//The Company.ThreeTierDemo.Components.Data.Admin.ShowAdmin is used to populate the datagrid.
		//
		//*********************************************************************
		private void BindDataGrid(String SortField)
		{
			System.Data.DataView MsDataView;
			Company.ThreeTierDemo.Components.Business.Admin ParamAdmin;

			//Create the Admin instance.
			ParamAdmin = new Company.ThreeTierDemo.Components.Business.Admin();
			MsDataView = ParamAdmin.ShowAll().Tables[0].DefaultView;
			MsDataView.Sort = SortField;
			MsDataGrid.DataSource = MsDataView;
			MsDataGrid.DataBind();
		}

		//*********************************************************************
		//
		// MsDataGrid_ItemDataBound event
		//
 		//It adds client side JavaScript event to the delete BoundColumn.
		//
		//*********************************************************************
		private void MsDataGrid_ItemDataBound(System.Object source, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			System.Web.UI.WebControls.LinkButton DeleteButton;
			if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
			{
				DeleteButton = (System.Web.UI.WebControls.LinkButton)e.Item.FindControl("DeleteButton");
				DeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this company?');");
			}
		}

		//*********************************************************************
		//
		// MsDataGrid_SortCommand event
		//
 		//The ViewState["SortExpression"] is Assigned the sort expression value.
 		//The datagrid is then populated using the BindDataGrid function.
		//
		//*********************************************************************
		private void MsDataGrid_SortCommand(System.Object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			ViewState["SortExpression"] = e.SortExpression;
			BindDataGrid(Convert.ToString(ViewState["SortExpression"]));
		}

		//*********************************************************************
		//
		// MsDataGrid_PageIndexChanged event
		//
 		//The CurrentPageIndex is Assigned the page index value.
 		//The datagrid is then populated using the BindDataGrid function.
		//
		//*********************************************************************
		private void MsDataGrid_PageIndexChanged(System.Object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			MsDataGrid.CurrentPageIndex = e.NewPageIndex;
			BindDataGrid(Convert.ToString(ViewState["SortExpression"]));
		}

		//*********************************************************************
		//
		// MsDataGrid_DeleteCommand event
		//
 		//The record is deleted.
 		//The datagrid is then populated using the BindDataGrid function.
		//
		//*********************************************************************
		private void MsDataGrid_DeleteCommand(System.Object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			Int32 AdminId;
			AdminId = 0;
			Int32 ReturnValue;
			if (MsDataGrid.EditItemIndex == e.Item.ItemIndex)
			{
				ErrorMessage.Text = "WARNING: Unable to delete record in edit mode!";
				MsDataGrid.EditItemIndex = -1;
				BindDataGrid(Convert.ToString(ViewState["SortExpression"]));
				return ;
			}
			PageMessage.Text = "";
			ErrorMessage.Text = "";
			AdminId = Convert.ToInt32(MsDataGrid.DataKeys[ e.Item.ItemIndex ]);
			ReturnValue = Company.ThreeTierDemo.Components.Data.Admin.DeleteAdmin(AdminId, User.Identity.Name);
			if (ReturnValue == 0)
			{
				Int32 LastPageItems;

				LastPageItems = MsDataGrid.Items.Count % MsDataGrid.PageSize;
				if (LastPageItems == 1 && MsDataGrid.PageCount > 1 )
				{
					MsDataGrid.CurrentPageIndex = MsDataGrid.PageCount - 2;
				}
				else 
				{
					MsDataGrid.CurrentPageIndex = 0;
				}

				ErrorMessage.Text = "Record deleted!";
			}
			else if (ReturnValue == -1)
			{
				ErrorMessage.Text = "Custom error!";
			}
			else 
			{
				PageMessage.Text = "Record does not exist!";
			}

			BindDataGrid(Convert.ToString(ViewState["SortExpression"]));
		}

		//*********************************************************************
		//
		// MsDataGrid_EditCommand event
		//
 		//The datagrid record to be edited is selected.
 		//The datagrid is then populated using the BindDataGrid function.
		//
		//*********************************************************************
		private void MsDataGrid_EditCommand(System.Object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			PageMessage.Text = "";
			ErrorMessage.Text = "";
			MsDataGrid.EditItemIndex = e.Item.ItemIndex;
			BindDataGrid(Convert.ToString(ViewState["SortExpression"]));
		}

		//*********************************************************************
		//
		// MsDataGrid_CancelCommand event
		//
 		//The record to be edited is canceled.
 		//The datagrid is then populated using the BindDataGrid function.
		//
		//*********************************************************************
		private void MsDataGrid_CancelCommand(System.Object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			PageMessage.Text = "";
			ErrorMessage.Text = "";
			MsDataGrid.EditItemIndex = -1;
			BindDataGrid(Convert.ToString(ViewState["SortExpression"]));
		}

		//*********************************************************************
		//
		// MsDataGrid_UpdateCommand event
		//
 		//The Company.ThreeTierDemo.Components.Data.Admin.updateAdmin is used to update the record.
 		//The datagrid is then populated using the BindDataGrid function.
		//
		//*********************************************************************
		private void MsDataGrid_UpdateCommand(System.Object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			System.Web.UI.WebControls.TextBox FirstNameField;
			System.Web.UI.WebControls.TextBox LastNameField;
			System.Web.UI.WebControls.TextBox UsernameField;
			System.Web.UI.WebControls.TextBox PasswordField;
			Int32 ReturnValue;
			Int32 AdminId;
			AdminId = 0;
			Company.ThreeTierDemo.Components.Business.Admin ParamAdmin;

			FirstNameField = (System.Web.UI.WebControls.TextBox)e.Item.FindControl( "FirstNameField");
			LastNameField = (System.Web.UI.WebControls.TextBox)e.Item.FindControl( "LastNameField");
			UsernameField = (System.Web.UI.WebControls.TextBox)e.Item.FindControl( "UsernameField");
			PasswordField = (System.Web.UI.WebControls.TextBox)e.Item.FindControl( "PasswordField");

			AdminId = Convert.ToInt32(MsDataGrid.DataKeys[ e.Item.ItemIndex ]);
			//Create the Admin instance.
			ParamAdmin = new Company.ThreeTierDemo.Components.Business.Admin();
			ParamAdmin.AdminId = AdminId;
			ParamAdmin.FirstName = FirstNameField.Text;
			ParamAdmin.LastName = LastNameField.Text;
			ParamAdmin.Username = UsernameField.Text;
			ParamAdmin.Password = PasswordField.Text;
			//Assign the return value.
			ReturnValue = ParamAdmin.Update(User.Identity.Name);
			if (ReturnValue == 0)
			{
				ErrorMessage.Text = "Record updated!";
			}
			else if (ReturnValue == -1)
			{
				ErrorMessage.Text = "Custom error!";
			}
			else 
			{
				PageMessage.Text = "Record already exists!";
			}
			MsDataGrid.EditItemIndex = -1;
			BindDataGrid(Convert.ToString(ViewState["SortExpression"]));
		}

	}
}

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
Software Developer (Senior)
Australia Australia
Stevan is a Microsoft Certified Solutions Developer in .Net Architecture (MCSD.Net Early Achiever – one among the first 2500 worldwide), Microsoft Certified Application Developer in .Net – MCAD.Net (Charter Member - one among the first 5000 developers worldwide).

Comments and Discussions