Click here to Skip to main content
15,896,727 members
Articles / Web Development / HTML

A Server Control Authoring JavaScript

Rate me:
Please Sign up or sign in to vote.
4.33/5 (5 votes)
12 Jun 20067 min read 38.3K   379   30  
This is a short series of articles about Abstract Programming. This part is a look at C# authoring a JavaScript file at design time.
// Copyright �  2005 - ProMatrix Inc.

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Text;
using ProMatrix.Lib.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.IO;

namespace ProMatrix.Lib.Controls
{
	public class ControlBase : System.Web.UI.WebControls.WebControl
	{
		protected string _title;
		protected string _onload;
		protected string _onclose;
		protected string _onpreclose;
		protected string _onclick;
		protected string _onchange;
		protected string _text;
		protected bool _hidden;
        protected bool _selected;
        protected Unit _width;
        protected bool _widthInPx;
		protected Unit _height;
        protected bool _heightInPx;
        protected Unit _top;
        protected Unit _left;
        protected string _id;
        protected string _domainid;
        protected string _userid;
        protected string _sqlid;
        protected string _sqlstring;
        protected string _cssFile;
		protected string _cssClass;
		protected string _language;
		protected string _nationality;
		protected string _action;
		protected string _tooltip;
        protected bool _runtimeUpdate;
        protected string _image;
        protected bool _scrolling;
        protected int _zindex;

        protected Unit _image_offset_L;
        protected Unit _image_offset_T;
        protected Unit _image_offset_W;
        protected Unit _image_offset_H;

		protected bool designMode;
		protected string iPath;
		protected HtmlTextWriter output;
		public Rendering Rendering;

		// there are ? types of paths
		// path to js paths
		// relative paths from webpage

		public const string wwwroot = "wwwroot";
		public const string htmSP = " ";
		public const string htmNL = "<br>";

		public static string ProMatrixLibPath = ConfigurationManager.AppSettings["ProMatrix.Lib"]; // file path
		public static string ProMatrixLibPath2 = ConfigurationManager.AppSettings["ProMatrix.Lib2"]; // web path

        public string ctrlImagePath = ProMatrixLibPath2 + "/Controls/Images/";
        public string libScriptsPath = ProMatrixLibPath2 + "/JScripts/";
        public string libDScriptsPath = ProMatrixLibPath2 + "/DScripts/";
        public string libStylesPath = ProMatrixLibPath2 + "/Styles/";
        public string dScriptsPath = ProMatrixLibPath + "/DScripts/";
		protected static string className = typeof(CMenuGroup).Name;
		protected string ctrlName = className + "_ctl";

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			init(Page);
		}

		protected override void Render(HtmlTextWriter output)
		{
			this.output = output;
			init(Page);
		}

		public void init(Page Page)
		{
			Rendering = new Rendering(Page, libStylesPath, libScriptsPath, libDScriptsPath);
			if (HttpContext.Current == null)
				designMode = true;
			else
				designMode = false;
				iPath = ctrlImagePath;
		}

        public void deleteDScript(string dScript)
        {
            File.Delete(dScriptsPath + dScript);
        }

        public string getSessionID(Page Page)
        {
            return Page.Session.SessionID;
        }

        protected object getSessionProperty(string propName, object defaultObj)
        {
            string s = _id + "_" + propName.ToString();
            if (Page.Session[s] == null)
                Page.Session[s] = defaultObj;
            return Page.Session[s];
        }

        protected void setSessionProperty(string propName, object defaultObj)
        {
            string s = _id + "_" + propName.ToString();
            Page.Session[s] = defaultObj;
        }

		protected string getDScriptName()
		{
			string s = MapPath();
			s = s.Substring(0, s.LastIndexOf("."));
			s = s.Substring(s.IndexOf(wwwroot) + wwwroot.Length + 1);
			s = s.Replace('\\', '.') + "." + _id;
			return s;
		}

        protected string getClassName()
        {
            return this.ToString();
        }

		#region MapPath
		/*
		MapPath("1234.aspx");		"c:\\inetpub\\wwwroot\\TestControls\\CMenu\\1234.aspx"
		MapPath("");						"c:\\inetpub\\wwwroot\\TestControls\\CMenu"
		MapPath();						"c:\\inetpub\\wwwroot\\TestControls\\CMenu\\Default.aspx"
		*/

		protected string MapPath()
		{ // returns full path and file name of webform
			if (designMode)
			{
				ControlDesignerHelper cdh = new ControlDesignerHelper();
				return cdh.ProjectDocument();
			}
			else
			{
				string FilePath = Page.Request.FilePath;
				FilePath = FilePath.Substring(FilePath.LastIndexOf("/") + 1);
				return Page.MapPath("") + "\\" + FilePath;
			}
		}
			
		protected string MapPath(string s)
		{	// mimics Page.MapPath
			if (designMode)
			{
				ControlDesignerHelper cdh = new ControlDesignerHelper();
				string FullPath = cdh.ProjectDocument();
				FullPath = FullPath.Substring(0, FullPath.LastIndexOf("\\"));

				if (s.Length > 0) FullPath += "\\";			
				
				return FullPath + s;
			}
			else
				return Page.MapPath(s);
		}
		#endregion

		public string getJSboolean(bool b)
		{
			if (b) return "true";
			else return "false";
		}
		
		public string getLibJScriptsPath()
		{
			return libScriptsPath;
		}
		
		protected string getStyleAttribute(string key)
		{	// return a style attribute
			IEnumerator keys = Style.Keys.GetEnumerator();
			while (keys.MoveNext())
			{
				string k = (String)keys.Current;
				if (key.ToLower() == k.ToLower())
					return Attributes.CssStyle[k];
			}
			return "";
		}

		protected string getStyleAll()
		{	// return this objects dimensions (width & height)
			return getStylePosition() + getStyleVisibility() + getZindex(); 
		
		}

		protected string getStylePosition()
		{	// return this objects dimensions (width & height)
			if (getStyleAttribute("position") == "absolute")
			{
				string left = getStyleAttribute("left");
				string top = getStyleAttribute("top");
				return "position:absolute; left:" + left + "; top:" + top + "; ";
			}
			return "";
		}

		protected string getStyleVisibility()
		{	// return this objects dimensions (width & height)
			if (_hidden)
				return "visibility:hidden; ";
			else
				return "";
		}

        protected string pxToVal(string px)
		{	// return this objects dimensions, omit th px
            if (px == "") return "";
            return px.Substring(0, px.Length - 2);
		}

        protected double pxToVal2(string px)
        {
            if (px == "") return 0;
            return Convert.ToInt64(pxToVal(px));
        }
        
        protected string getZindex()
		{	// return this objects dimensions (width & height)
			return "z-index:" + getStyleAttribute("z-index") + "; ";
		}

		protected string getStyleDim()
		{	// render outside of style to maintain compatibility
			return "width:" + _width.Value + "px; " + "height:" + _height.Value + "px; ";
		}

		protected string getDim()
		{
			return "width='" + _width.Value + "px" + "' height='" + _height.Value + "px" + "' ";
		}

		protected void renderDesignModeStart()
		{
			if(designMode)
				output.WriteLine("			<div " + getDim() + "style='" + getStylePosition() + getStyleDim() + "' " + " >");
		}

		protected void renderDesignModeStop()
		{
			if (designMode)
				output.WriteLine("			</div>");
		}

		protected string getRuntimePosition()
		{
			if (designMode == false)
				return getStylePosition();
			else
				return "";
		}

		protected void renderText(bool bTreatQuotes)
		{	// render text from include file... or _text
			if (designMode)
			{
				output.WriteLine(_text);
				return;
			}
			output.WriteLine(RenderText(_text, bTreatQuotes));
		}

		public string RenderText(string _text, bool bTreatQuotes)
		{
			// determine if _text includes a file name. If so return text in file.
			if (_text.IndexOf("#include") == -1)
			{
				return _text;
			}

			string fileText = "";
			// find filename
			int begfileIndex = _text.IndexOfAny(new char[] { '\"', '\'' }) + 1;
			int endfileIndex = _text.LastIndexOfAny(new char[] { '\"', '\'' });
			String path = _text.Substring(begfileIndex, endfileIndex - begfileIndex);
			path = MapPath(path);
			FileInfo FileInfo = new FileInfo(path);
			if (FileInfo.Exists == false)
			{
				return "Error: The include file could not be found!";
			}

			try
			{
				// Create an instance of StreamReader to read from a file.
				// The using statement also closes the StreamReader.
				StreamReader sr = new StreamReader(path);
				// Read and display lines from the file until the end of 
				// the file is reached.
				string line;
				while ((line = sr.ReadLine()) != null)
				{
					if (bTreatQuotes == false)
					{
						fileText += line;
					}
					else
					{
						char[] charArray = line.ToCharArray();
						for (int i = 0; i < charArray.Length; i++)
						{
							if (charArray[i] == '\'')
								fileText += "\\" + "'";
							else
								if (charArray[i] == '\"')
									fileText += "\\" + "\"";
								else
									fileText += charArray[i];
						}
					}
				}
			}
			catch (Exception)
			{
				return "Error: The include file could not be read!";
			}
			return fileText;
		}
	
	}
}

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
United States United States
My formal education was in Electronic Engineering with a background in real-time data acquisition for aerospace. Early in my career, I switched my focus from hardware engineering to software engineering. Most, but not all, of my development experience has been using Microsoft technologies and tools. My current accomplishments are web applications using ASP.Net, SQL Server 2005, JavaScript, and Visual Studio Tools for Office. I enjoy all facets of a development life cycle, and have played almost all roles. Most recently, I have been involved in project technical leadership, architecture, and training, and I have created several small courses including this one on Abstract Programming.

Comments and Discussions