数字文本控件

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
 
namespace PublicControls
{
     ///<summary>
     /// DateTimeBox 的摘要说明。
     ///</summary>
     public class IntTextBox : Control,IPostBackDataHandler
     {
         #region 预定义
 
         private int min;
         private int max;
         private Unit width;
         private Unit height;
         private int defaultnum;
 
         ///<summary>
         /// 委托页面索引事件
         ///</summary>
         public event EventHandler OnValueChanged;
 
         #endregion
        
         #region 属性
        
         ///<summary>
         /// 最小值
         ///</summary>
         [Description(" 最小值")]
         public int MinNum
         {
              get
              {
                   if(this.IsNumeric(ViewState["MinNum"]))
                   {
                       min = (int)ViewState["MinNum"];
                   }
                   return min;
              }
              set
              {
                   min = value;
                   ViewState["MinNum"] = min;
              }
         }
 
         ///<summary>
         /// 最大值
         ///</summary>
         [Description(" 最大值")]
         public int MaxNum
         {
              get
              {
                   if(this.IsNumeric(ViewState["MaxNum"]))
                   {
                       max = (int)ViewState["MaxNum"];
                   }
                   return max;
              }
              set
              {
                   max = value;
                   ViewState["MaxNum"] = max;
              }
         }
 
         ///<summary>
         ///
         ///</summary>
         [Description(" 值")]
         public int DefaultNum
         {
              get
              {
                   if(this.IsNumeric(ViewState["DefaultNum"]))
                   {
                       defaultnum = (int)ViewState["DefaultNum"];
                   }
                   return defaultnum;
              }
              set
              {
                   defaultnum=value;
                   ViewState["DefaultNum"] = defaultnum;
              }
         }
 
         ///<summary>
         /// 宽度
         ///</summary>
         [Description(" 宽度")]
         public Unit Width
         {
              get
              {
                   return width;
              }
              set
              {
                   width = value;
              }
         }
 
         ///<summary>
         /// 高度
         ///</summary>
         [Description(" 高度")]
         public Unit Height
         {
              get
              {
                   return height;
              }
              set
              {
                   height = value;
              }
         }
         #endregion
 
         #region 事件或方法
         ///<summary>
         /// 数据回送事件
         ///</summary>
         ///<param name="postDataKey"></param>
         ///<param name="values"></param>
         ///<returns></returns>
         public bool LoadPostData(string postDataKey, NameValueCollection values)
         {
              if(!this.IsNumeric(values[this.UniqueID]))
              {
                   return false;
              }
             
              if(DefaultNum != int.Parse(values[this.UniqueID]))
              {
                DefaultNum = int.Parse(values[this.UniqueID]);
                  
                   if(DefaultNum>MaxNum)
                   {
                       DefaultNum = MaxNum;
                   }
                   if(DefaultNum<MinNum)
                   {
                       DefaultNum = MinNum;
                   }
                  
                   return true;
              }
 
              return false;
         }
 
         ///<summary>
         /// 当由类实现时,用信号要求服务器控件对象通知 ASP.NET 应用程序该控件的状态已更改。
         ///</summary>
         public void RaisePostDataChangedEvent()
         {
             
              ValueChanged();
             
         }       
 
         ///<summary>
         /// 将事件委托给所调用的页面
         ///</summary>
         public void ValueChanged()
         {
              if(OnValueChanged!=null)
              {
                   OnValueChanged(this,System.EventArgs.Empty);
              }
          }
         #endregion
 
         #region 输出
         ///<summary>
         /// 将此控件呈现给指定的输出参数。
         ///</summary>
         ///<param name="output"> 要写出到的 HTML 编写器 </param>
         protected override void Render(HtmlTextWriter output)
         {
              string view;
              view=ViewString(MinNum,MaxNum);
              output.Write(view);
         }
         #endregion
 
         #region 其他函数或方法
         ///<summary>
         /// 输出字符串组合
         ///</summary>
         ///<param name="min"></param>
         ///<param name="max"></param>
         ///<returns></returns>
         string ViewString(int min,int max)
         {
              string view="";
              view+="<script><!--/n";
              view+="function changetext(direction)/n";
              view+="{/n";
              view+="if(direction=='up')/n";
              view+="{/n";
              view+="if(document.all."+this.UniqueID+".value<"+max+")/n";
              view+="document.all."+this.UniqueID+".value=parseInt(document.all."+this.UniqueID+".value)+1;/n";
              view+="}/n";
              view+="else/n";
              view+="{/n";
              view+="if(document.all."+this.UniqueID+".value>"+min+")/n";
              view+="document.all."+this.UniqueID+".value=parseInt(document.all."+this.UniqueID+".value)-1;/n";
              view+="}/n";
              view+="}/n";
 
              view+="function keydown()/n";
              view+="{/n";
              view+="var KeyCode=event.keyCode;/n";
              view+="if(((KeyCode>47)&&(KeyCode<58))||(KeyCode==8)||(KeyCode==46)||(KeyCode==37)||(KeyCode==39)||(KeyCode>95&&KeyCode<106))/n";
              view+="{/n";
              view+="return true;/n";
              view+="}/n";
              view+="else/n";
              view+="{/n";
              view+="return false;/n";
              view+="}/n";
              view+="}/n";
 
              view+="function focusblur()/n";
              view+="{/n";
              view+="var num=document.all."+this.UniqueID+".value;/n";
              view+="if(num>"+MaxNum+")/n";
              view+="{/n";
              view+="document.all."+this.UniqueID+".value="+MaxNum+";/n";
              view+="}/n";
              view+="if(num<"+MinNum+")/n";
              view+="{/n";
              view+="document.all."+this.UniqueID+".value="+MinNum+";/n";
              view+="}/n";
              view+="}/n";
 
              view+="//--></script>";
 
              view+="<div><span onclick=/"changetext('down')/" style=/"BORDER-RIGHT: black thin solid; BORDER-TOP: black thin solid; BORDER-LEFT: black thin solid; CURSOR: hand; BORDER-BOTTOM: black thin solid;">6</span><input type=text id=/""+this.UniqueID+"/" name=/""+this.UniqueID+"/" value=/""+this.DefaultNum+"/" style=/"width:"+this.Width+";height:"+this.Height+"/" onkeydown=/"return keydown()/" onblur=/"focusblur()/"><span onclick=/"changetext('up')/" style=/"BORDER-RIGHT: black thin solid; BORDER-TOP: black thin solid; BORDER-LEFT: black thin solid; CURSOR: hand; BORDER-BOTTOM: black thin solid;">5</span></div>";
              return view;
         }
 
 
         ///<summary>
         /// 判断是否为数字字符串
         ///</summary>
         ///<param name="obj"> 需验证的字符串 </param>
         ///<returns> 判断结果,符合条件为True,不符合条件为False </returns>
         public bool IsNumeric(object obj)
         {
 
              obj += "";
              string str = obj.ToString();
             
              // 判断是否为空
              if (str == null || str.Length==0)
              {
                   return false;
              }
              // 循环检查每个字符
              foreach(char c in str)
              {
                   if (!Char.IsNumber(c))
                   {
                       return false;
                   }
              }
              return true;
         }
 
         #endregion
     }
}
 

你可能感兴趣的:(数字)