Click here to Skip to main content
15,886,578 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.5K   2.8K   103  
Use form controls as static informational popups or movable control windows.
using System;
using System.Collections;
using System.Configuration;
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;

namespace CarboniSoftware.PopExample
{
	/// <summary>
	/// Summary description for MainPage.
	/// </summary>
	public class MainPage : System.Web.UI.Page
	{
		string msg;
		protected System.Web.UI.WebControls.Label lblPopInfo01;
		protected System.Web.UI.WebControls.Label lblPopInfo02;
		protected System.Web.UI.WebControls.Label lblPopInfo03;
		protected System.Web.UI.HtmlControls.HtmlAnchor aPopInfo01;
		protected System.Web.UI.HtmlControls.HtmlAnchor aPopInfo02;
		protected System.Web.UI.HtmlControls.HtmlAnchor aPopInfo03;
		protected System.Web.UI.HtmlControls.HtmlGenericControl PopHelpArea;
		protected System.Web.UI.HtmlControls.HtmlAnchor btnPop01;
		protected System.Web.UI.WebControls.Label Label1;
		public Hashtable	InfoLinks = new Hashtable();
		protected System.Web.UI.HtmlControls.HtmlGenericControl PopArea;
		protected System.Web.UI.WebControls.ListBox ListBox1;
		private CollapseControl cc;

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Load the information text for the popups from XML file
			try
			{
				string infoLinksFileName = HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings["infoLinksFile"]);
				InfoLinkItems infoLinkItems = new InfoLinkItems();
				infoLinkItems.ReadXml(infoLinksFileName);
				foreach (InfoLinkItems.InfoLinkItemRow ir in infoLinkItems.InfoLinkItem.Select("", ""))
				{
					this.InfoLinks.Add(ir.ID, ir);
				}
			}
			catch(Exception ex)
			{
				msg = ex.Message;
			}

			// Create the info popups
			CreateInfoPopup("InfoLink01", aPopInfo01);
			CreateInfoPopup("InfoLink02", aPopInfo02);
			CreateInfoPopup("InfoLink03", aPopInfo03);

			CreateCollapseControl();
		
		}

		/// <summary>
		/// CreateInfoPopup loads the hidden info popup control and populates
		/// the text from XML element identified.
		/// </summary>
		/// <param name="InfoLinkID">The ID of the info element in XML file</param>
		/// <param name="btnID">The object that, when clicked, will display info popup.</param>
		private void CreateInfoPopup(string InfoLinkID, HtmlAnchor btnID)
		{
			Control PopArea = Page.FindControl("PopHelpArea");

			InfoLinkItems.InfoLinkItemRow infoLink = 
				(InfoLinkItems.InfoLinkItemRow)this.InfoLinks[InfoLinkID];
			string popText  = infoLink.InfoLinkItem_Text;
			string helpCtrl = "PopUpHelp.ascx";
			PopUpHelp p = (PopUpHelp)LoadControl(helpCtrl);
			string PopUpID = infoLink.popID;
			p.SetPopUpID(PopUpID);
			p.SetPopUpText(popText);
			if (PopArea != null)
				PopArea.Controls.Add(p);

			btnID.Attributes.Add("onclick", "return !showPop('" + PopUpID + "', " + ListBox1.ClientID + ",event,true,0,0,false,null,true);");
		}

		private void CreateInitialItems()
		{
			ArrayList Items = new ArrayList();
			ArrayList ItemIDs = new ArrayList();
			Hashtable Groups = new Hashtable();
			ArrayList GroupOrder = new ArrayList();

			Items.Add("Item 1");
			Items.Add("Item 2");
			Items.Add("Item 3");
			Items.Add("Item 4");
			Items.Add("Item 5");
			Items.Add("Item 6");
			Items.Add("Item 7");
			Items.Add("Item 8");
			Items.Add("Item 9");
			Items.Add("Item 10");

			ItemIDs.Add("I01");
			ItemIDs.Add("I02");
			ItemIDs.Add("I03");
			ItemIDs.Add("I04");
			ItemIDs.Add("I05");
			ItemIDs.Add("I06");
			ItemIDs.Add("I07");
			ItemIDs.Add("I08");
			ItemIDs.Add("I09");
			ItemIDs.Add("I10");

			Session["Items"] = Items;
			Session["ItemIDs"] = ItemIDs;
			Session["Groups"] = Groups;
			Session["GroupOrder"] = GroupOrder;
		}

		private void LoadListBox(ArrayList Items, ArrayList ItemIDs, Hashtable Groups)
		{
			ArrayList DisplayItems = new ArrayList();
			ArrayList RemoveIDs = new ArrayList();

			if (Groups.Count > 0)
			{
				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++)
							RemoveIDs.Add(ItemIDs.IndexOf(GItems[i]));
					}

					string GroupDisplay = GName.Substring(1) + " (";
					for (int i=0; i<GItems.Count; i++)
					{
						GroupDisplay += Items[ItemIDs.IndexOf(GItems[i])];
						if (i != GItems.Count-1)
							GroupDisplay += ", ";
					}
					GroupDisplay += ")";
					DisplayItems.Add(GroupDisplay);

				}
			}

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

			ListBox1.DataSource = DisplayItems;
			ListBox1.DataBind();
		}

		private void CreateCollapseControl()
		{
			if (!this.IsPostBack || (Session["Items"] == null))
				CreateInitialItems();
			LoadListBox((ArrayList)Session["Items"], (ArrayList)Session["ItemIDs"], (Hashtable)Session["Groups"]);

			ArrayList Items = (ArrayList)Session["Items"];
			ArrayList ItemIDs = (ArrayList)Session["ItemIDs"];
			Hashtable Groups = (Hashtable)Session["Groups"];
			ArrayList GroupOrder = (ArrayList)Session["GroupOrder"];

			cc = (CollapseControl)LoadControl("CollapseControl.ascx");
			cc.ID = this.ID + "_cc_1";
			cc.CollapsePopulate(cc.ClientID, "Items", Items, ItemIDs, Groups, GroupOrder);
			cc.ColDoneClick += new EventHandler(cc_ColDoneClick);

			Control PopArea = Page.FindControl("PopArea");
			Control cc1 = PopArea.FindControl(cc.ClientID);
			if (cc1 != null)
				PopArea.Controls.Remove(cc1);
			PopArea.Controls.Add(cc);

			/////////////////////////////////////////////////////////////
			// Bug fix for IE 6.x and earlier only.  Selection controls 
			// have invalid z-orders, and must be hidden by script if
			// popup window overlaps any part of the control.  Javascript
			// method showPop must know the ids of the selection controls
			// used on this form.  They are passed here as an array of
			// clientIDs.  Javascript function will test for overlap and
			// hide the controls if necessary.
			string sbCtrlIDs = "['";
			// ADD YOUR SELECTION CONTROLS HERE: example>>>>
			//sbCtrlIDs += ddlMyDropList.ClientID;
			//sbCtrlIDs += "', '";
			//sbCtrlIDs += ddlMyListBox.ClientID;
			sbCtrlIDs += "']";
			
			// Add script to register controls with javascript variables
			Control jsc = Page.FindControl("JSArea");
			if (jsc != null)
			{
				LiteralControl jlc = new LiteralControl("<script language='javascript'>registerIEWindowCtrls(" + sbCtrlIDs + ");</script>\r\n");
				jsc.Controls.Add(jlc);
			}
			//
			/////////////////////////////////////////////////////////////
			btnPop01.Attributes.Add("onclick", "return !showPop('" + cc.ClientID + "_CollapseContainer'," + ListBox1.ClientID + ",event,false,-10,-30,true," +sbCtrlIDs+ ",false);");
		}

		#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

		private void cc_ColDoneClick(object sender, EventArgs e)
		{
			if (sender.GetType().Name != "CollapseControl_ascx")
				return;

			CollapseControl ccSender = (CollapseControl)sender;

			Session["Groups"] = ccSender.NewGroups;
			Session["GroupOrder"] = ccSender.NewGroupOrder;

			CreateCollapseControl();
		}

	}
}

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