Step 1
Add the source code in WebControl Library Project.
Step 2
Analize the source code.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace Personal.Web.UI.WebControls
{
public class TextBoxMask :System.Web.UI.WebControls.TextBox
{
public TextBoxMask()
{
}
#region Event TextChanged
protected override void OnTextChanged(EventArgs e)
{
TextChange(this,e);
}
public virtual void TextChange(object sender,EventArgs e)
{
string txt = "";
if (this.Text.Trim() != "")
{
double val = double.Parse(this.ParserValue(this.Text));
txt = this.Format(val,this.ImputMask);
}
this.Text = txt;
if (this.ReleaseMask)
{
this.ImputMask = "";
}
base.OnTextChanged(e);
}
#endregion
protected override void Render(HtmlTextWriter output)
{
this.AutoPostBack = true;
base.Render(output);
}
protected string Format(double number,string mask)
{
string ret = "";
ret = number.ToString(mask);
return ret;
}
protected string ParserValue(string Value)
{
int i=0;
string val = "";
string ret = "";
for (i=0;i<Value.Length;i++)
{
val = Value.Substring(i,1);
if (char.IsDigit(Value,i))
{
ret += val;
}
}
return ret;
}
private string imputmask;
public virtual string ImputMask
{
set
{
imputmask = value;
}
get
{
return imputmask;
}
}
public string ClipText
{
get
{
if (this.Text != null || this.Text.Trim() !="")
{
return this.ParserValue(this.Text);
}
else
{
return "";
}
}
}
}
}
Last Step
- Compile.
- Customize Toolbox and add reference to assembly.
- Drag-Drop the
TextboxMask
control to page.
- Run the application.
Something appears similar to the figure.