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

ASP.Net User Controls as Static or Movable PopUps

Rate me:
Please Sign up or sign in to vote.
4.63/5 (21 votes)
2 Feb 2007CPOL2 min read 110.6K   2.8K   103  
Use form controls as static informational popups or movable control windows.
namespace CarboniSoftware.PopExample
{
	using System;
	using System.Collections;
	using System.Data;
	using System.Drawing;
	using System.Text;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using skmExtendedControls;

	/// <summary>
	///		The CollapseControl is a popup-type control that lives on the web
	///		page as a hidden div.  Javascript is used to display the control
	///		when the user selects a button or link to activate.  The collapse
	///		functionality is created with elements in the hidden div that are
	///		manipulated using javascript.  When the user completes collapse
	///		selections, a post-back is performed, and the group information
	///		is retrieved from a hidden input area on the control.
	/// </summary>
	public class CollapseControl : System.Web.UI.UserControl
	{
		public String CheckListID;
		public ArrayList ItemLabels;
		public ArrayList ItemIDs;
		public ArrayList NewGroupOrder;
		public Hashtable NewGroups;
		protected skmCheckBoxList chkListItems;
		protected System.Web.UI.WebControls.TextBox txtGroupName;
		protected System.Web.UI.HtmlControls.HtmlGenericControl CollapseGroupListNames;
		protected System.Web.UI.HtmlControls.HtmlAnchor btnCollapseDups;
		protected System.Web.UI.HtmlControls.HtmlAnchor btnCollapseRemove;
		protected System.Web.UI.HtmlControls.HtmlAnchor btnCollapseStartOver;
		protected System.Web.UI.WebControls.ImageButton btnCollapseDone;
		protected System.Web.UI.HtmlControls.HtmlImage btnCollapseStartOverImg;
		protected System.Web.UI.HtmlControls.HtmlImage btnCollapseDupsImg;
		protected System.Web.UI.HtmlControls.HtmlImage btnCollapseRemoveImg;
		protected System.Web.UI.WebControls.TextBox txtGroupList;
		protected System.Web.UI.WebControls.PlaceHolder CollapseJSArea;
		protected System.Web.UI.WebControls.Label lblCollapse;
		protected System.Web.UI.HtmlControls.HtmlGenericControl CollapseContainer;
		public bool bCollapseDupsAllowed = true;
		public string closeImage = "";
		public string closeHover = "";
		public ArrayList RemoveIDs = new ArrayList();

		public CollapseControl()
		{
			ItemLabels = new ArrayList();
			ItemIDs = new ArrayList();
			NewGroupOrder = new ArrayList();
			NewGroups = new Hashtable();
		}

		public void CollapseReset()
		{
			NewGroupOrder.Clear();
			NewGroups.Clear();
		}

		public void CollapsePopulate(string colPopID, string Title, ArrayList Names, ArrayList IDs, Hashtable Groups, ArrayList GroupOrder)
		{
			lblCollapse.Text = Title;

			CheckListID = colPopID + "_checkList";
			ItemLabels = Names;
			ItemIDs = IDs;

			ArrayList CollapsedIDs = new ArrayList(IDs);

			string GName;
			ArrayList GItems;
			IEnumerator en = Groups.GetEnumerator();
			en.Reset();
			while (en.MoveNext())
			{
				GName = (string)((DictionaryEntry)en.Current).Key;
				GItems = (ArrayList)((DictionaryEntry)en.Current).Value;

				bool bRemoveItems = (GName.Substring(0, 1) == "R") ? true : false;
				if (bRemoveItems)
				{
					for (int i=0; i<GItems.Count; i++)
						if (CollapsedIDs.Contains(GItems[i]))
							CollapsedIDs.Remove(GItems[i]);
				}

			}

			for (int i=0; i<ItemIDs.Count; i++)
			{
				if (!CollapsedIDs.Contains(ItemIDs[i]))
					RemoveIDs.Add(i);
			}

			NewGroups = Groups;
			NewGroupOrder = GroupOrder;

			this.btnCollapseDone.Click += new System.Web.UI.ImageClickEventHandler(this.btnCollapseDone_Click);
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			string ImagePath = "images/";
			btnCollapseDone.ImageUrl = ImagePath + "collapse_btn_4sm.gif";

			closeImage = ImagePath + "close_gray.gif";
			closeHover = ImagePath + "close_gray_hvr.gif";

			txtGroupName.Text = "New Group Name";
			txtGroupName.Enabled = false;
			txtGroupList.EnableViewState = true;
			txtGroupList.Enabled = false;

			// Set the attributes for the buttons
			btnCollapseStartOverImg.Src = ImagePath + "collapse_btn_1sm.gif";
			btnCollapseStartOverImg.Attributes.Add("onclick", "event.cancelBubble=true; onCollapseStartOver('" + chkListItems.ClientID +  "', '" + txtGroupName.ClientID + "', '" + btnCollapseStartOverImg.ClientID + "', '" + btnCollapseDupsImg.ClientID + "', '" + btnCollapseRemoveImg.ClientID + "', '" + CollapseGroupListNames.ClientID + "', '" + txtGroupList.ClientID + "');");
			btnCollapseStartOverImg.Attributes.Add("onmouseover", "style.cursor='pointer'; setmsg('Reset form'); this.src = '" + ImagePath + "collapse_strt_hvr.gif'");
			btnCollapseStartOverImg.Attributes.Add("onmouseout", "style.cursor='default'; setmsg(''); this.src = '" + ImagePath + "collapse_btn_1sm.gif'");
			btnCollapseDupsImg.Disabled = true;
			btnCollapseDupsImg.Src = ImagePath + "collapse_dups_dis.gif";
			if (bCollapseDupsAllowed)
			{
				btnCollapseDupsImg.Attributes.Add("onclick", "event.cancelBubble=true; onCollapseDups('" + chkListItems.ClientID +  "', '" + txtGroupName.ClientID + "', '" + btnCollapseDupsImg.ClientID + "', '" + btnCollapseRemoveImg.ClientID + "', '" + CollapseGroupListNames.ClientID + "', '" + txtGroupList.ClientID + "');");
				btnCollapseDupsImg.Attributes.Add("onmouseover", "style.cursor='pointer'; setmsg('Create Group without Removing Items');");
				btnCollapseDupsImg.Attributes.Add("onmouseout", "style.cursor='default'; setmsg('');");
			}
			btnCollapseRemoveImg.Disabled = true;
			btnCollapseRemoveImg.Src = ImagePath + "collapse_rem_dis.gif";
			btnCollapseRemoveImg.Attributes.Add("onclick", "event.cancelBubble=true; onCollapseRemove('" + chkListItems.ClientID +  "', '" + txtGroupName.ClientID + "', '" + btnCollapseDupsImg.ClientID + "', '" + btnCollapseRemoveImg.ClientID + "', '" + CollapseGroupListNames.ClientID + "', '" + txtGroupList.ClientID + "');");
			btnCollapseRemoveImg.Attributes.Add("onmouseover", "style.cursor='pointer'; setmsg('Create Group and Remove Items');");
			btnCollapseRemoveImg.Attributes.Add("onmouseout", "style.cursor='default'; setmsg('');");
			btnCollapseDone.Attributes.Add("onmouseover", "style.cursor='pointer'; setmsg('Save and Close'); this.src = '" + ImagePath + "collapse_done_hvr.gif'");
			btnCollapseDone.Attributes.Add("onmouseout", "style.cursor='default'; setmsg(''); this.src = '" + ImagePath + "collapse_btn_4sm.gif'");

			// Build the checklist of available items that will get passed to javascript
			string JS = "<script language='javascript'>\r\n";
			JS += "var " + CheckListID + "Items = [";

			chkListItems.Items.Clear();
			for (int i=0; i<ItemLabels.Count; i++)
			{
				chkListItems.Items.Add(new ListItem((string)ItemLabels[i], (string)ItemIDs[i]));
				chkListItems.Items[i].Attributes.Add("defaultValue", (string)ItemIDs[i]);
				chkListItems.Items[i].Attributes.Add("onclick", "onCollapseItemChecked(event, '" + chkListItems.ClientID + "', '" + chkListItems.ClientID + "_" + i.ToString() + "', '" + txtGroupName.ClientID + "', '" + btnCollapseDupsImg.ClientID + "', '" + btnCollapseRemoveImg.ClientID + "');");
			
				JS += "'" + chkListItems.ClientID + "_" + i.ToString() + "'";
				if (i+1<ItemLabels.Count)
					JS += ", ";
			}

			///////////////////////////////////////////////////////////////////
			// Workaround for Firefox - when user clicks on checkbox label,
			// event is not fired for checkbox.  Therefore, an event must be 
			// fired for entire list.
			chkListItems.Attributes.Add("onclick", "onCollapseListChecked(event, '" + chkListItems.ClientID + "', '" + txtGroupName.ClientID + "', '" + btnCollapseDupsImg.ClientID + "', '" + btnCollapseRemoveImg.ClientID + "');");
			///////////////////////////////////////////////////////////////////

			// Create the array of various button images that will be used
			// by the collapse control
			StringBuilder sbImgs = new StringBuilder(1000);
			sbImgs.Append("['");
			sbImgs.Append(ImagePath + "collapse_btn_1sm.gif', '");
			sbImgs.Append(ImagePath + "collapse_btn_2sm.gif', '");
			sbImgs.Append(ImagePath + "collapse_btn_3sm.gif', '");
			sbImgs.Append(ImagePath + "collapse_btn_4sm.gif', '");
			sbImgs.Append(ImagePath + "collapse_strt_hvr.gif', '");
			sbImgs.Append(ImagePath + "collapse_dups_hvr.gif', '");
			sbImgs.Append(ImagePath + "collapse_rem_hvr.gif', '");
			sbImgs.Append(ImagePath + "collapse_done_hvr.gif', '");
			sbImgs.Append(ImagePath + "collapse_btn_1sm.gif', '");
			sbImgs.Append(ImagePath + "collapse_dups_dis.gif', '");
			sbImgs.Append(ImagePath + "collapse_rem_dis.gif', '");
			sbImgs.Append(ImagePath + "collapse_btn_4sm.gif']");

			// As existing groups need to be known so the names are not duplicated,
			// create an array of existing group names to pass to javascript
			StringBuilder sbGroups = new StringBuilder();
			if (NewGroups.Count > 0)
			{
				int nGroupCount = NewGroups.Count;
				sbGroups.Append("['");

				for (int iNG=0; iNG<NewGroupOrder.Count; iNG++)
				{
					string NewGroupName = (string)NewGroupOrder[iNG];
					sbGroups.Append(NewGroupName);
					nGroupCount--;
					if (nGroupCount > 0)
						sbGroups.Append("', '");
				}
				sbGroups.Append("']");
			}
			else
			{
				sbGroups.Append("null");
			}

			// Build a list of existing group items to remove from checklist
			string RItems = "[";
			for (int iR=0; iR<RemoveIDs.Count; iR++)
			{
				RItems += "'" + chkListItems.ClientID + "_" + ((int)RemoveIDs[iR]).ToString() + "'";
				if ((iR+1)<RemoveIDs.Count)
					RItems += ", ";
			}
			RItems += "]";

			// Build the javascript that gets run for collapse control creation
			string s = (bCollapseDupsAllowed) ? "true" : "false";
			JS += "];\r\n";
			JS += "loadCollapsePop('" + chkListItems.ClientID + "', " + CheckListID + "Items, " + s + ", " + sbImgs.ToString() + ", " + sbGroups.ToString() + ", '" + CollapseGroupListNames.ClientID + "', " + RItems + ");\r\n";
			//JS += "onCollapseStartOver('" + chkListItems.ClientID +  "', '" + txtGroupName.ClientID + "', '" + btnCollapseStartOverImg.ClientID + "', '" + btnCollapseDupsImg.ClientID + "', '" + btnCollapseRemoveImg.ClientID + "', '" + CollapseGroupListNames.ClientID + "', '" + txtGroupList.ClientID + "');\r\n";
			JS += "</script>";

			// Add the javascript to the page
			CollapseJSArea.Controls.Clear();
			LiteralControl lc = new LiteralControl(JS);
			CollapseJSArea.Controls.Add(lc);

		}

		#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);

		}
		#endregion

		public event EventHandler ColDoneClick;

		protected void OnColDoneClick(EventArgs e)
		{
			if(ColDoneClick != null)
				ColDoneClick(this, e);
		}     

		private void btnCollapseDone_Click(object sender, ImageClickEventArgs e)
		{
			string groupNames = txtGroupName.Text;
			string groupList = txtGroupList.Text;

			if (txtGroupList.Text == "!!")
			{
				NewGroupOrder.Clear();
				NewGroups.Clear();
				txtGroupList.Text = txtGroupList.Text.Substring(2);
			}

			string GroupDelim = "$";
			char[] delim = GroupDelim.ToCharArray();
			string[] Groups = groupList.Split(delim);

			string GroupName = "";
			string ItemIDDelim = ",";
			delim = ItemIDDelim.ToCharArray();
			string[] ItemIDs = null;

			foreach (string s in Groups)
			{
				if (s.Length > 0)
				{
					int nItemListStart = s.IndexOf('[');
					if (nItemListStart == -1)
						continue;
					GroupName = s.Substring(0, nItemListStart);
					if (s.Length > nItemListStart+5)
					{
						string group = s.Substring(nItemListStart+1);
						group = group.Substring(0, group.Length-4);

						ItemIDs = group.Split(delim);

						ArrayList ar = new ArrayList();
						foreach (string itemID in ItemIDs)
						{
							if (itemID.Length > 0)
								ar.Add(itemID);
						}

						NewGroupOrder.Add(GroupName);
						NewGroups[GroupName] = ar;
					}
				}
			}

			OnColDoneClick(e);
		}
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Software architect and developer with over 20 years of experience, specializing in GUI designs and intranet systems in MFC/C++ and ASP.Net using C#.

Comments and Discussions