Skip Navigation Links.

Northwind\WebControls\WUCBase.cs

D:\Temp3\TestProjects\CSADO.Net\Northwind\WebControls\WUCBase.cs
/*
 * This file was generated by ProCG version 2.0
 *
 * File name:	Northwind\WebControls\WUCBase.cs
 * Language:	C# - ADO.Net 
 * Database:	My Sql
 *
 * Copyright (c) 2002-2019 iGenXSoft.
 * For more information visit http://www.igenxsoft.com
 */


using System;
using System.Text;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Northwind.General;
using Northwind.Data;

namespace Northwind.WebControls
{
	/// <summary>
	/// Summary description for NorthwindWUCBase.
	/// </summary>
	[ParseChildren(true)]
	[PersistChildren(false)]
	[Designer(typeof(Northwind.WebControls.NorthwindWebControlDesigner))]
	public abstract class NorthwindWUCBase : Northwind.WebControls.NorthwindUserControl, ISupportInitialize
	{
		protected CStructData m_Data;
		protected CStructData m_OriginData;
		protected NorthwindToolBarWCC m_TopToolBar;
		protected NorthwindToolBarWCC m_ToolBar;
		protected string m_ElementTitle;
		protected bool m_DesignMode = (System.Web.HttpContext.Current == null);
		protected bool m_bInsideBeginInit;
		protected EditModeEnum m_PageEditMode;
		protected EditModeEnum m_UserEditMode = 0;

		protected string m_HeaderText = String.Empty;
		protected Hashtable eventsFired;

		public delegate void GeneralWebEventHandler(Object sender, GeneralWebEvent args);
		public event GeneralWebEventHandler OnGeneralWebEvent;

		public NorthwindWUCBase()
		{
			InitAll();
		}

		protected virtual void InitAll()
		{
			m_Data = null;
			m_OriginData = null;

			DataObjectsContainer = null;
		}


		[Browsable(true)]
		[NotifyParentProperty(true)]
		public virtual  string HeaderText
		{
			set
			{
				m_HeaderText = value;
			}

			get
			{
				return (m_HeaderText);
			}
		}



		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			if (UserEditMode != 0 && UserEditMode != EditMode)
			{
				EditMode = UserEditMode;
				SetControlsDynamicAttributes();
				SetControlsValues();
			}

			if (this.Page.IsPostBack)
			{
				if (this.Visible)
				{
					WebEnv.ContainerControl = this;
					WebEnv.OnPostback();
				}
			}
		}
		[Browsable(false)]
		public bool HasDynamicFields
		{
			get { return true; }
		}



		[Browsable(false)]
		protected CStructData FreshData
		{
			get
			{
				m_Data = null;
				return (Data);
			}
		}

		[Browsable(false)]
		protected CStructData NewData
		{
			set
			{

				Data = value;
				KeepOrigin();
			}

		}


		public void KeepOrigin()
		{
			OriginData = Data.Clone();
		}

		[Browsable(false)]
		public CStructData OriginData
		{
			get
			{
				if (DataInstanceName == "")
					throw new Exception("DataInstanceName must be specified");

				if (DataObjectsContainer != null)
				{

					m_OriginData = (CStructData)DataObjectsContainer[DataInstanceName + "OriginData"];
				}
				else
				{
					if (KeepDataInSession)
						m_OriginData = (CStructData)this.Page.Session[DataInstanceName + "OriginData"];
					else
						m_OriginData = (CStructData)this.ViewState[DataInstanceName + "OriginData"];
				}

				return (m_OriginData);
			}
			set
			{
				if (DataInstanceName == "")
					throw new Exception("DataInstanceName must be specified");

				m_OriginData = value;
				if (DataObjectsContainer != null)
				{

					DataObjectsContainer[DataInstanceName + "OriginData"] = m_OriginData;
				}
				else
				{
					if (KeepDataInSession)
						this.Page.Session[DataInstanceName + "OriginData"] = m_OriginData;
					else
						this.ViewState[DataInstanceName + "OriginData"] = m_OriginData;
				}
			}
		}



		[Browsable(true)]
		[NotifyParentProperty(true)]
		public bool ShowToolBar
		{
			get
			{
				if (ViewState["ShowToolBar"] == null)
				{
					ShowToolBar = false;
				}
				return ((bool)ViewState["ShowToolBar"]);
			}
			set
			{

				ViewState["ShowToolBar"] = value;
			}
		}


		[Browsable(true)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[NotifyParentProperty(true)]
		public NorthwindToolBarWCC ToolBar
		{
			get
			{

				if (ShowToolBar)
				{

					if (m_ToolBar != null)
						return m_ToolBar;
					else
					{
						m_ToolBar = (NorthwindToolBarWCC)NorthwindWebEnv.FindControlRecursive(this, "NorthwindToolBarWCC1");
					}
					return m_ToolBar;
				}
				else
					return null;
			}

			set
			{
				m_ToolBar = value;
				m_ToolBar.Page = this.Page;
			}
		}

		[Browsable(true)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[NotifyParentProperty(true)]
		public NorthwindToolBarWCC TopToolBar
		{
			get
			{
				if (m_TopToolBar != null)
					return m_TopToolBar;
				else
				{
					m_TopToolBar = (NorthwindToolBarWCC)NorthwindWebEnv.FindControlRecursive(this, "TopToolbar");
				}
				return m_TopToolBar;
			}

			set
			{
				m_TopToolBar = value;
				m_TopToolBar.Page = this.Page;
			}
		}

		protected virtual void SetEventsForToolbarItems()
		{
			NorthwindToolBarWCC toolbar = ToolBar;
			ImageButton copyButton = toolbar.CopyButton;
			if (copyButton != null)
			{
				copyButton.Click += new ImageClickEventHandler(copyButton_Click);
			}

			ImageButton pastButton = toolbar.PasteButton;
			if (pastButton != null)
			{
				pastButton.Click += new ImageClickEventHandler(pastButton_Click);
			}

			ImageButton discardChangesButton = toolbar.DiscardChangesButton;

			if (discardChangesButton != null)
			{

				discardChangesButton.Click += new ImageClickEventHandler(discardChangesButton_Click);
			}

			ImageButton saveButton = toolbar.SaveButton;
			if (saveButton != null)
			{
				saveButton.Click += new ImageClickEventHandler(saveButton_Click);
			}

			ImageButton deleteButton = toolbar.DeleteLinesButton;
			if (deleteButton != null)
			{
				deleteButton.Click += new ImageClickEventHandler(deleteButton_Click);
			}

			ImageButton printButton = toolbar.PrintButton;
			if (printButton != null)
			{
				printButton.OnClientClick = "CallPrint(this,'" + this.ClientID + "');return false;";
			}

			ImageButton digButton = toolbar.DigButton;
			if (digButton != null)
			{
				digButton.Click += new ImageClickEventHandler(digButton_Click);
			}

		}


		protected virtual void digButton_Click(object sender, ImageClickEventArgs e)
		{
			NorthwindPage espPage = (NorthwindPage)this.Page;

			string digUrl = WebEnv.EntityPageUrl + @"?AcmNum=1&AcmParams=" + (Data as CRecordData).GetPrimaryKeyAsString() + "&Collection=0";
			if (NorthwindGeneral.DigIntoSamePlace)
				espPage.Redirect(digUrl, true);
			else
				espPage.GetShowEntityTextBoxControl().Text = digUrl; // tells the form to open this page on load
		}


		protected void copyButton_Click(object sender, ImageClickEventArgs e)
		{
			Copy();
		}

		public void Copy()
		{
			CStructData structData;
			this.Page.Session["Clipboard"] = Data.CopyText;
		}

		protected void pastButton_Click(object sender, ImageClickEventArgs e)
		{
			Past();
		}

		public void Past()
		{
			string clipBoardText = (string)Page.Session["Clipboard"];

			if (clipBoardText != null)
			{

				if (IsDataIsRecordData())
					((CRecordData)Data).Past(clipBoardText);
				else
					Data.Past(clipBoardText);
			}
			SetControlsValues();
		}


		protected void discardChangesButton_Click(object sender, ImageClickEventArgs e)
		{
			if (!IsEventWasFired("DiscardChanges"))
			{
				DiscardChanges();
				EventWasFired("DiscardChanges");
			}
		}

		public void DiscardChanges()
		{
			if (OriginData != null)
			{
				Data = OriginData.Clone();
				SetControlsValues();
			}
		}

		protected void saveButton_Click(object sender, ImageClickEventArgs e)
		{
			if (!IsEventWasFired("Save"))
			{
				Save();
				SetControlsValues(); // to get auto incremenet fields and calculated fields
				EventWasFired("Save");
			}
		}


		public void Save()
		{
			GetControlsValues();
			if (!(Data is CRecordData))
				return;
			bool needToBeSaved = ((CRecordData)Data).NeedToBeSaved();
			if (!needToBeSaved)
			{
				AddInformationMessage("Nothing changed");
				return;
			}

			if (this.DataManager == null)
			{
				this.DataManager = (this.Page as NorthwindPage).DataManager;
			}
			try
			{
				bool needToInsert = ((CRecordData)Data).NeedToBeInserted();
				bool needToUpdated = ((CRecordData)Data).NeedToBeUpdated();
				bool needToDeleted = ((CRecordData)Data).NeedToBeDeleted();
				if (needToInsert || needToUpdated)
				{
					string message = "";
					// vv 
					//bool validate = ((CRecordData)Data).Validate(ref message);
					//if (!validate)
					//{
					//	this.AddErrorMessage("Validation Error: " + message);
					//	return;
					//}
				}
				DataManager.BeginTrans();
				DataManager.Save((CRecordData)Data);
				DataManager.CommitTrans();

				DataManager.CommitStatusAfterSave((CRecordData)Data);
				if (!CollectionDataInstanceName.Equals(string.Empty))
					SetDataToCollection(Data);
				Data = Data; // to keep it in session
				KeepOrigin();
				if (needToInsert)
				{
					AddInformationMessage(string.Format("{0} was inserted successfully", (ElementTitle == null) ? "Data" : ElementTitle));
					AfterInsert();
				}
				else if (needToDeleted)
				{
					AddInformationMessage(string.Format("{0} was deleted successfully", (ElementTitle == null) ? "Data" : ElementTitle));
					AfterDelete();
				}
				else
				{
					AddInformationMessage(string.Format("{0} was saved successfully", (ElementTitle == null) ? "Data" : ElementTitle));
					AfterUpdate();
				}
			}
			catch (Exception ex)
			{
				if (DataManager != null)
				{
					if (DataManager.IsInsideTransaction)
					{
						DataManager.RollbackTrans();
					}
				}
				AddErrorMessage(string.Format("Failed to save {0} <br> Error: {1}", (ElementTitle == null) ? "data" : ElementTitle, ex.Message));
			}
		}

		virtual protected void AfterInsert()
		{
		}

		virtual protected void AfterUpdate()
		{
		}

		virtual protected void AfterDelete()
		{
		}

		protected void deleteButton_Click(object sender, ImageClickEventArgs e)
		{
			if (!IsEventWasFired("Delete"))
			{
				Delete();
				SetControlsValues(); // to get auto incremenet fields and calculated fields
				EventWasFired("Delete");
			}
		}


		public virtual void Delete()
		{
			GetControlsValues();
			if (!(Data is CRecordData))
				return;

			((CRecordData)Data).SetDeleted();
			this.AddInformationMessage("Was marked as deleted");
		}


		public virtual void SetControlsValues()
		{
			throw new NotImplementedException("SetControlsValues()");
		}

		public virtual void GetControlsValues()
		{
			throw new NotImplementedException("GetControlsValues()");
		}

		public virtual void SetControlsDynamicAttributes()
		{
			throw new NotImplementedException("SetControlsDynamicAttributes()");
		}

		public bool IsDataIsRecordData()
		{
			if (Data.GetType().IsSubclassOf(typeof(CRecordData)))
				return (true);
			else
				return (false);
		}



		public virtual void Recreate()
		{
			SetControlsDynamicAttributes();
			SetControlsValues();
		}


		public virtual void HideAll()
		{
			throw new NotImplementedException("HideAll()");
		}


		public virtual void ShowAll()
		{
			throw new NotImplementedException("ShowAll()");
		}

		public virtual void ShowSummary()
		{
			throw new NotImplementedException("ShowSummary()");
		}

		public virtual void ShowAllExceptSummary()
		{
			throw new NotImplementedException("ShowAllExceptSummary()");
		}

		#region ISupportInitialize Members

		public void BeginInit()
		{
			m_bInsideBeginInit = true;
		}

		public void EndInit()
		{

			m_bInsideBeginInit = false;
		}

		#endregion


		[Browsable(false)]
		public CNorthwindUser User
		{
			get
			{
				if (this.Page.Session["User"] != null)
					return (CNorthwindUser)this.Page.Session["User"];
				else
					return null;
			}
		}


		protected virtual void SendGeneralWebEvent(GeneralWebEvent.EventTypeEnum eventType)
		{
			// Has any objects registered interest with our event? 
			if (OnGeneralWebEvent != null)
			{
				GeneralWebEvent generalWebEvent = new GeneralWebEvent();
				generalWebEvent.EvenetType = eventType;
				// Yes, notify all the objects in the delegate linked list
				OnGeneralWebEvent(this, generalWebEvent);
			}
		}


		[NotifyParentProperty(true)]
		public bool AllowEdit
		{
			get
			{
				if (Attributes["AllowEdit"] == null)
				{
					if (EditMode == EditModeEnum.ALL || EditMode == EditModeEnum.UPDATE)
						return true;
					else
						return false;
				}
				return (Attributes["AllowEdit"] == "yes");
			}
			set { Attributes["AllowEdit"] = (value ? "yes" : "no"); }


		}

		[NotifyParentProperty(true)]
		public bool AllowDelete
		{
			get
			{
				if (Attributes["AllowDelete"] == null)
				{
					if (EditMode == EditModeEnum.ALL)
						return true;
					else
						return false;
				}
				return (Attributes["AllowDelete"] == "yes");
			}
			set { Attributes["AllowDelete"] = (value ? "yes" : "no"); }
		}

		public virtual void RecreateControls()
		{
			this.Recreate();
		}

		public string ElementTitle
		{
			get
			{
				if (m_ElementTitle != null)
					return m_ElementTitle;
				if (ViewState["ElementTitle"] == null)
					return null;
				return (string)ViewState["ElementTitle"];
			}
			set
			{
				ViewState["ElementTitle"] = value;
			}
		}

		protected override void LoadViewState(object savedState)
		{
			base.LoadViewState(savedState);

			if (ViewState["UserEditMode"] != null)
			{
				m_UserEditMode = (EditModeEnum)ViewState["UserEditMode"];
			}
			if (this.Controls.Count == 0)
				Recreate();

		}

		public EditModeEnum EditMode
		{
			get
			{
				if (ViewState["EditMode"] == null)
				{
					if (!DesignMode && SessionManager.UserExist) // maybe session expired
					{
						if (SessionManager.UserExist && SessionManager.User.Type == (short)UserTypeEnum.DEBUG)
							return EditModeEnum.ALL;
						else
							return EditModeEnum.ALL;
					}
					else
						return EditModeEnum.VIEW_ONLY;

				}
				return (EditModeEnum)ViewState["EditMode"];
			}
			set
			{
				if (EditMode != value)
				{
					ViewState["EditMode"] = value;
				}
			}
		}

		[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public EditModeEnum UserEditMode
		{
			get
			{
				if (m_UserEditMode != 0)
					return m_UserEditMode;

				if (ViewState["UserEditMode"] != null)
				{
					return (EditModeEnum)ViewState["UserEditMode"];
				}
				return 0;
			}
			set
			{
				ViewState["UserEditMode"] = value;
				m_UserEditMode = value;
			}
		}


		public virtual void CustomToolbarItemControl_Click(object sender, EventArgs e)
		{
			(this.Page as NorthwindPage).OnCustomToolbarItemControlClicked(sender,this as NorthwindWUC);
		}

		virtual protected void AddInformationMessage(string informationMessage)
		{
			ToolBar.AddInformationMessage(informationMessage);
		}

		virtual protected void SetInformationMessage(string informationMessage)
		{
			ToolBar.SetInformationMessage(informationMessage);
		}

		virtual protected void AddErrorMessage(string errorMessage)
		{
			ToolBar.AddErrorMessage(errorMessage);
		}

		virtual protected void SetErrorMessage(string errorMessage)
		{
			ToolBar.SetErrorMessage(errorMessage);
		}

		virtual public void ClearAllMessages()
		{
			ToolBar.ClearAllMessages();
		}

		protected bool IsEventWasFired(string eventname)
		{
			if (eventsFired == null)
				return false;
			if (eventsFired[eventname] != null)
				return true;
			else
				return false;
		}

		protected void EventWasFired(string eventname)
		{
			if (eventsFired == null)
				eventsFired = new Hashtable();
			eventsFired[eventname] = "1";
		}
		protected override void OnDataBinding(EventArgs e)
		{
			Recreate();
			base.OnDataBinding(e);
		}

	}
}


  //     16378 ProCG uses this line - don't edit it