把哪天纯数字化文本框的WEB控件代码贴出来。

发表于:2007-06-30来源:作者:点击数: 标签:
using System; using System.Web.UI; using System.ComponentModel; using System.Web.UI.WebControls; using System.Text; using System.Text.RegularExpressions; namespace Blood.Com.WebControl { /// summary /// ASP.NET纯数字文本框控件 /// Blood 制
using System;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Text;
using System.Text.RegularExpressions;

namespace Blood.Com.WebControl
{
    /// <summary>
    /// ASP.NET纯数字文本框控件
    /// Blood 制作于2002年2月
    /// </summary>
    [ToolboxData("<{0}:NumberBox runat=server></{0}:NumberBox>"),DefaultProperty("DecimalPlaces")]
    public class NumericBox : TextBox
    {
        private int intDecimalPlaces = 0;
        private char chrDecimalSymbol = @#.@#;
        private bool bolAllowNegatives = true;

        /// <summary>
        /// 返回和设置数字文本框的十进制数字
        /// </summary>
        [Bindable(true),Category("Appearance"),    DefaultValue(0),]
        public virtual int DecimalPlaces
        {
            get { return intDecimalPlaces; }
            set { intDecimalPlaces = value; }
        }

        /// <summary>
        /// 返回和设置数字文本框的数字分组
        /// </summary>
        [Bindable(true),Category("Appearance"),DefaultValue("."),]
        public virtual char DecimalSymbol
        {
            get { return chrDecimalSymbol; }
            set { chrDecimalSymbol = value; }
        }

        /// <summary>
        /// 返回和设置是否可以显示负数
        /// </summary>
        [Bindable(true),Category("Appearance"),DefaultValue(true),]
        public virtual bool AllowNegatives
        {
            get { return bolAllowNegatives; }
            set { bolAllowNegatives = value; }
        }

        /// <summary>
        /// 返回和设置数字文本框的值
        /// </summary>
        public virtual double Value
        {
            get
            {
                try
                {
                    return ParseStringToDouble(this.Text);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            set
            {
                if ((value < 0) & !AllowNegatives)
                {
                    throw new ArgumentOutOfRangeException("Only positive values are allowed for this NumberBox");
                }
                base.Text = value.ToString(GetFormat()).Replace(".", DecimalSymbol.ToString());
            }
        }

        /// <summary>
        /// 返回和设置数字文本框的内容
        /// </summary>
        override public string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                try
                {
                    this.Value = ParseStringToDouble(value);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
            
        /// <summary>
        ///    为数字文本框添加一个控制键盘被按下后事件的脚本
        /// </summary>
        /// <param name="e"></param>
        override protected void OnPreRender(EventArgs e)
        {
            if (this.Page.Request.Browser.JavaScript == true)
            {
                // 添加JS脚本
                StringBuilder s = new StringBuilder();
                s.Append("\n<script type=@#text/javascript@# language=@#JavaScript@#>\n");
                s.Append("<!--\n");
                s.Append("    function NumberBoxKeyPress(event, dp, dc, n) {\n");
                s.Append("         var myString = new String(event.srcElement.value);\n");
                s.Append("         var pntPos = myString.indexOf(String.fromCharCode(dc));\n");
                s.Append("         var keyChar = window.event.keyCode;\n");
                s.Append("       if ((keyChar < 48) || (keyChar > 57)) {\n");
                s.Append("          if (keyChar == dc) {\n");
                s.Append("              if ((pntPos != -1) || (dp < 1)) {\n");
                s.Append("                  return false;\n");
                s.Append("              }\n");
                s.Append("          } else \n");            
                s.Append("if (((keyChar == 45) && (!n || myString.length != 0)) || (keyChar != 45)) \n");
                s.Append("             return false;\n");
                s.Append("       }\n");
                s.Append("       return true;\n");
                s.Append("    }\n");
                s.Append("// -->\n");
                s.Append("</script>\n");
                
                // 为页面添加脚本
                this.Page.RegisterClientScriptBlock("NumberBoxKeyPress", s.ToString());

                // 添加键盘按下事件
                try
                {
                    this.Attributes.Remove("onKeyPress");
                }
                finally
                {
                    this.Attributes.Add("onKeyPress", "return NumberBoxKeyPress(event, "
                        + DecimalPlaces.ToString() + ", "
                        + ((int)DecimalSymbol).ToString() + ", "
                        + AllowNegatives.ToString().ToLower() + ")");
                }
            }
        }
        
        /// <summary>
        /// 使用RegularExpressionValidator控件返回一个正则表达式字符串的验证
        /// </summary>
        virtual public string ValidationRegularExpression
        {
            get
            {
                StringBuilder regexp = new StringBuilder();
                
                if (AllowNegatives)
                    regexp.Append("([-]|[0-9])");
                
                regexp.Append("[0-9]*");
                
                if (DecimalPlaces > 0)
                {
                    regexp.Append("([");
                    regexp.Append(DecimalSymbol);
                    regexp.Append("]|[0-9]){0,1}[0-9]{0,");
                    regexp.Append(DecimalPlaces.ToString());
                    regexp.Append("}$");
                }
                
                return regexp.ToString();
            }
        }

        /// <summary>
        /// 将String类型解析为Double类型
        /// </summary>
        /// <param name="s">String类型以被解析为Double类型</param>
        /// <returns>Double值</returns>
        virtual protected double ParseStringToDouble(string s)
        {
            s = s.Replace(DecimalSymbol.ToString(), ".");
            return double.Parse(s);
        }        
        
        /// <summary>
        /// 为数字文本框显示内容格式化字符串
        /// </summary>
        /// <returns>格式化字符串</returns>
        virtual protected string GetFormat()
        {
            StringBuilder f = new StringBuilder();
            f.Append("0");
            if (DecimalPlaces > 0)
            {
                f.Append(".");
                f.Append(@#0@#, DecimalPlaces);
            }
            return f.ToString();
        }
    }
}

原文转自:http://www.ltesting.net