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

JavaScriptBuilder: JavaScript Handler Class for Custom Controls

Rate me:
Please Sign up or sign in to vote.
4.74/5 (39 votes)
2 Sep 200311 min read 184.4K   5.4K   109  
The conflict between maintenance and efficiency examined and resolved.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using CP.WebControls;

[assembly: TagPrefix("CP.WebControls", "cpccnt")]

namespace CP.WebControls
{
	/// <summary>
	/// Summary description for ClickCounter.
	/// </summary>
	[
	DefaultProperty("StartValue"), 
	ToolboxData("<{0}:ClickCounter runat=server></{0}:ClickCounter>")
	]
	public class ClickCounter : System.Web.UI.WebControls.WebControl
	{
		/// <summary>
		/// The initial value of the counter
		/// </summary>
		[
		Bindable(true), 
		Category("Appearance"), 
		DefaultValue(0),
		Description("The initial value of the counter")
		] 
		public int StartValue
		{
			get
			{
				if (! (ViewState["StartValue"] is int))
					ViewState["StartValue"] = 0;
				return (int) ViewState["StartValue"];
			}

			set
			{
				ViewState["StartValue"] = value;
			}
		}

		/// <summary>
		/// Prefix text
		/// </summary>
		[
		Bindable(true), 
		Category("Appearance"), 
		DefaultValue("Counter:"),
		Description("Prefix Text")
		] 
		public string Text
		{
			get
			{
				if (! (ViewState["Text"] is string))
					ViewState["Text"] = "Counter:";
				return (string) ViewState["Text"];
			}

			set
			{
				ViewState["Text"] = value;
			}
		}

		[
		DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
		]
		protected virtual string IncrementScriptName
		{
			get
			{
				return "ClickCounter_IncrementValue";
			}
		}

		[
		DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
		]
		protected virtual string IncrementScript
		{
			get
			{
#if DEBUG
		JavaScriptBuilder jsb = new JavaScriptBuilder(true);
#else
		JavaScriptBuilder jsb = new JavaScriptBuilder();
#endif
		jsb.AddCommentLine("Splits the inner text into Text (txt) and Counter (c) parts,");
		jsb.AddCommentLine("increments (c) and joins them back together again.");
		jsb.AddLine("function ", IncrementScriptName, "(elmt)");

		jsb.OpenBlock(); // function (elmt)
		jsb.AddCommentLine("Initialize variables");
		jsb.AddLine("var inner = elmt.innerText;");
		jsb.AddLine("var c = 0;");
		jsb.AddLine("var txt = \"\";");

		jsb.AddLine();

		jsb.AddCommentLine("Run through inner text string");
		jsb.AddLine("for (idx = 0; idx < inner.length; idx++)");

		jsb.OpenBlock(); // for (idx...)
		jsb.AddCommentLine("Split string into text and counter parts");
		jsb.AddLine("c = parseInt( inner.substring (idx, inner.length + 1) );");
		jsb.AddLine("txt = inner.substring(0, idx);");

		jsb.AddLine();

		jsb.AddCommentLine("If we have a number, get out of the loop");
		jsb.AddLine("if ( ! isNaN( c ) )");

		jsb.OpenBlock(); // if ( ! isNaN( c ) )
		jsb.AddLine("break;");
		jsb.CloseBlock(); // if ( ! isNaN( c ) )
				
		jsb.CloseBlock(); // for (idx...)

		jsb.AddLine();
		jsb.AddCommentLine("Increment counter");
		jsb.AddLine("c++;");
		jsb.AddCommentLine("Rebuild the string and put it in the inner text"); 
		jsb.AddLine("elmt.innerText = txt + \" \" + c;");
		jsb.CloseBlock(); // function (elmt)

		return jsb.ToString();
			}
		}

		/// <summary> 
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		protected override void Render(HtmlTextWriter output)
		{
			if (!Page.IsStartupScriptRegistered(IncrementScriptName))
				Page.RegisterStartupScript(IncrementScriptName, IncrementScript);

			output.AddAttribute(HtmlTextWriterAttribute.Onclick, String.Concat(IncrementScriptName, "(this);"));
			this.RenderBeginTag(output);

			output.Write(Text.Trim());
			output.Write(" ");
			output.Write(StartValue.ToString());

			this.RenderEndTag(output);
		}
	}
}

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
Web Developer
United Kingdom United Kingdom
Paul lives in the heart of En a backwater village in the middle of England. Since writing his first Hello World on an Oric 1 in 1980, Paul has become a programming addict, got married and lost most of his hair (these events may or may not be related in any number of ways).

Since writing the above, Paul got divorced and moved to London. His hair never grew back.

Paul's ambition in life is to be the scary old guy whose house kids dare not approach except at halloween.

Comments and Discussions