|
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.