Click here to Skip to main content
15,880,956 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.2K   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.
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;

namespace ProMatrix.Lib.UI
{
	public class Rendering
	{
		public Page Page;
		string libStylesPath;
		string libScriptsPath;
		string libDScriptsPath;

		public Rendering(Page Page, string libStylesPath, string libScriptsPath, string libDScriptsPath)
		{
			this.Page = Page;
			this.libStylesPath = libStylesPath;
			this.libScriptsPath = libScriptsPath;
			this.libDScriptsPath = libDScriptsPath;
		}

		//public void RenderNoScriptInfo() // just after opening form tag
		//{
		//    if (!this.IsStartupScriptRegistered("NoScriptInfo"))
		//    {
		//        StringBuilder loadScripts = new StringBuilder();
		//        loadScripts.Append("<noscript ><br>");
		//        loadScripts.Append("<table border='0' width='99%' id='table1' height='142' style='position: absolute; left:4px; top:2px; height:100%; width:100%; z-index:500' bgcolor='#FFFFFF'>");
		//        loadScripts.Append("<tr>");
		//        loadScripts.Append("<td align='left' valign='top'>");
		//        loadScripts.Append("<p align='center'>JavaScript Error: This application requires JavaScript.<br>");
		//        loadScripts.Append("Your browser must have JavaScript enabled for this application to function.<br>");
		//        loadScripts.Append("<a href='" + findFolder("calendar") + "ProMatrix.Common/FAQ/browserSettings.htm'>Click here for details on how to ");
		//        loadScripts.Append("enable JavaScript for this application.</a><br>");
		//        loadScripts.Append("&nbsp;</p>");
		//        loadScripts.Append("</td>");
		//        loadScripts.Append("</tr>");
		//        loadScripts.Append("</table>");
		//        loadScripts.Append("</noscript><br>");
		//        RegisterClientScriptBlock("NoScriptInfo", loadScripts.ToString());
		//    }
		//}

		#region Shortcut icon
		//

		#endregion

		#region Stylesheet
		public void StylesheetFile(string id) // in the Styles folder
		{
			string cssFile = libStylesPath + id;
			StylesheetFile(id, cssFile);
		}
		
		public void StylesheetFile(string id, string cssFile) // in the header
		{
			cssFile = cssFile.ToLower() + ".css";
			HtmlHead HtmlHead = null;
			for (int i = 0; i < Page.Controls.Count; i++)
			{
				Type t = Page.Controls[i].GetType();
				if (Page.Controls[i].GetType().ToString() == "System.Web.UI.HtmlControls.HtmlHead")
				{
					HtmlHead = (HtmlHead)Page.Controls[i];
					for (int j = 0; j < HtmlHead.Controls.Count; j++)
					{
						Control Control = (Control)HtmlHead.Controls[j];
						if (Control.GetType().ToString() == "System.Web.UI.LiteralControl")
						{
							LiteralControl LiteralControl = (LiteralControl)HtmlHead.Controls[j];
							if(LiteralControl.Text.IndexOf("href='" + cssFile + "'") != -1)
								return;
						}
					}
				}
			}

			if (HtmlHead != null)
				HtmlHead.Controls.Add(new LiteralControl("<link id='" + id + "' href='" + cssFile + "' rel='stylesheet' type='text/css' /> \r\n"));
		}
		#endregion

		#region JavaScript
		public void StartupLibJsFile(string scriptName)
		{
			StartupJsFile(libScriptsPath + scriptName);
		}

		public void StartupLibDJsFile(string scriptName)
		{
			StartupJsFile(libDScriptsPath + scriptName);
		}

        public void StartupDefaultJsFile(string sDefault) 
        {  // only use with served page
            string CurrentExecutionFilePath = Page.Request.CurrentExecutionFilePath.Substring(0, Page.Request.CurrentExecutionFilePath.LastIndexOf('/'));
            StartupJsFile(CurrentExecutionFilePath + "/JScripts/" + sDefault);
        }

		public void StartupJsFile(string scriptName) // just after opening form tag
		{
			scriptName = scriptName.ToLower() + ".js";
			StringBuilder loadScripts = new StringBuilder();
			loadScripts.Append("<script type='text/javascript' language='JavaScript' src='" + scriptName + "'></script>\r\n");
			Page.ClientScript.RegisterClientScriptBlock(typeof(String), scriptName, loadScripts.ToString());
		}


        public void EndingLibJsFile(string scriptName)
        {
            EndingJsFile(libScriptsPath + scriptName);
        }

		public void EndingJsFile(string scriptName) // just before closing form tag
		{
			scriptName = scriptName.ToLower() + ".js";
			StringBuilder loadScripts = new StringBuilder();
			loadScripts.Append("<script type='text/javascript' language='JavaScript' src='" + scriptName + "'></script>");
			Page.ClientScript.RegisterStartupScript(typeof(String), scriptName, loadScripts.ToString());
		}

		public void StartupJsCode(string scriptName, string script) // just after opening form tag
		{
			scriptName = scriptName.ToLower();
			StringBuilder loadScripts = new StringBuilder();
			loadScripts.Append("<script language='JavaScript'>\r\n" + script + "\r\n</script>\r\n\r\n");
			Page.ClientScript.RegisterClientScriptBlock(typeof(String), scriptName, loadScripts.ToString());
		}

		public void EndingJsCode(string scriptName, string script) // just before closing form tag
		{
			scriptName = scriptName.ToLower();
			StringBuilder loadScripts = new StringBuilder();
			loadScripts.Append("<script language='JavaScript'>\r\n" + script + "\r\n</script>\r\n\r\n");
			Page.ClientScript.RegisterStartupScript(typeof(String), scriptName, loadScripts.ToString());
		}

		public void DivOpening(string scriptName, string style)
		{
			StringBuilder loadScripts = new StringBuilder();
			loadScripts.Append("<div style='" + style + "'>\r\n");
			Page.ClientScript.RegisterClientScriptBlock(typeof(String), scriptName, loadScripts.ToString());
		}

		public void DivClosing(string scriptName)
		{
			StringBuilder loadScripts = new StringBuilder();
			loadScripts.Append("</div>\r\n\r\n");
			Page.ClientScript.RegisterClientScriptBlock(typeof(String), scriptName, loadScripts.ToString());
		}

		public string concatScriptCommand(string arg, string val)
		{
			return arg + " = \"" + val + "\";\r\n";
		}

		public string concatScriptCommand(string arg, int val)
		{
			return arg + " = " + val + ";\r\n";
		}
		#endregion


	}
}

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