using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
namespace HuizhongLibrary.Control
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:MaskedTextBox runat=server>{0}:MaskedTextBox>")]
public class MaskedTextBox : TextBox
{
// private InputMode m_InputMode = InputMode.None;
///
/// 输入模式
///
[Description("输入模式"), DefaultValue("None"), Category("扩展"), NotifyParentProperty(true)]
public virtual InputMode InputMode
{
get
{
if (this.ViewState["InputMode"]==null){return InputMode.None;}
else{return (InputMode)this.ViewState["InputMode"]; }
}
set
{
this.ViewState["InputMode"] = value;
}
}
// private float m_MinValue=0;
///
/// 最小值
///
[Description("最小值"), DefaultValue("0"), Category("扩展"), NotifyParentProperty(true)]
public virtual float MinValue
{
get
{
if (this.ViewState["MinValue"]==null){return 0;}
else{return (float)this.ViewState["MinValue"]; }
}
set
{
this.ViewState["MinValue"] = value;
}
}
// private float m_MaxValue=float.MaxValue;
///
/// 最大值
///
[Description("最大值"), DefaultValue("0"), Category("扩展"), NotifyParentProperty(true)]
public virtual float MaxValue
{
get
{
if (this.ViewState["MaxValue"]==null){return float.MaxValue;}
else{return (float)this.ViewState["MaxValue"]; }
}
set
{
this.ViewState["MaxValue"] = value;
}
}
public MaskedTextBox()
{
//this.PreRender += new EventHandler(MaskedTextBox_PreRender);
this.Init += new EventHandler(MaskedTextBox_Init);
}
void MaskedTextBox_Init(object sender, EventArgs e)
{
if (this.ReadOnly == true)
{
this.ReadOnly = false;
Attributes.Add("readOnly", "true");
}
Attributes.Add("autocomplete", "off");
if (this.InputMode == InputMode.DateTime) Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "TextCalendar", JavaScriptConstant.DateMonth);
if (this.InputMode == InputMode.Float) Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "KeyDown_float", JavaScriptConstant.KeyDown_float);
if (this.InputMode == InputMode.Int) Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "KeyDown_int", JavaScriptConstant.KeyDown_int);
if (this.InputMode == InputMode.Float || this.InputMode == InputMode.Int || this.InputMode == InputMode.MyFloat) Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "CheckSize", JavaScriptConstant.CheckSize);
Registeronkeyup();
}
void MaskedTextBox_PreRender(object sender, EventArgs e)
{
if (this.ReadOnly == true)
{
this.ReadOnly = false;
Attributes.Add("readOnly", "true");
}
Attributes.Add("autocomplete", "off");
if (this.InputMode==InputMode.DateTime)Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "TextCalendar", JavaScriptConstant.DateMonth);
if (this.InputMode == InputMode.Float) Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "KeyDown_float", JavaScriptConstant.KeyDown_float);
if (this.InputMode == InputMode.Int) Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "KeyDown_int", JavaScriptConstant.KeyDown_int);
if (this.InputMode == InputMode.Float || this.InputMode == InputMode.Int||this.InputMode==InputMode.MyFloat) Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "CheckSize", JavaScriptConstant.CheckSize);
Registeronkeyup();
}
#region 注册判断脚本
private void Registeronkeyup()
{
if (this.Attributes["MinValue"] == null)
{
this.Attributes.Add("MinValue", this.MinValue.ToString());
this.Attributes.Add("MaxValue", this.MaxValue.ToString());
}
else
{
this.Attributes["MinValue"]=this.MinValue.ToString();
this.Attributes["MaxValue"] = this.MaxValue.ToString();
}
//onkeyup
switch (this.InputMode)
{
case InputMode.DateTime:
if (this.Attributes["onfocus"] == null) { this.Attributes.Add("onfocus", "setday(this);"); }
else { this.Attributes["onfocus"] = "setday(this);" + this.Attributes["onfocus"].ToString(); }
break;
case InputMode.Int:
if (this.Attributes["onkeydown"] == null) { this.Attributes.Add("onkeydown", "KeyDown_int(this,this.MinValue);"); }
if (this.Attributes["onkeyup"] == null) { this.Attributes.Add("onkeyup", "CheckSize(this,this.MinValue,this.MaxValue);"); }
if (this.Attributes["onblur"] == null) { this.Attributes.Add("onblur", "CheckMinSize(this,this.MinValue);"); }
break;
case InputMode.Float:
if (this.Attributes["onkeydown"] == null) { this.Attributes.Add("onkeydown", "KeyDown_float(this,this.MinValue);"); }
if (this.Attributes["onkeyup"] == null) { this.Attributes.Add("onkeyup", "CheckSize(this,this.MinValue,this.MaxValue);"); }
if (this.Attributes["onblur"] == null) { this.Attributes.Add("onblur", "CheckMinSize(this,this.MinValue);"); }
break;
default:
break;
}
if (this.InputMode == InputMode.None) return;
if (this.Attributes["style"] == null) { this.Attributes.Add("style", "ime-mode: disabled;"); }
else
{
if (Regex.IsMatch(this.Attributes["style"].ToString(), "ime-mode.{1}") == false) this.Attributes["style"] = "ime-mode: disabled;" + this.Attributes["style"].ToString();
}
}
#endregion
}
public enum InputMode
{
None = 0,
DateTime = 1,
Int = 2,
Float = 3,
MyFloat = 4
}
}