数字文本控件

原创 2006年05月22日 14:50:00
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
     }
}
 
版权声明:本文为博主原创文章,未经博主允许不得转载. dawngrp.com

C#数字文本控件原码

  • 2010年09月05日 00:04
  • 15KB
  • 下载

基于jquery扩展文本——之精度文本控件(数字,精度文本)

在我做项目过程中常常会遇到文本控件值只存在数字类型或浮点类型,比如数量,价格。常常我都需要为这值做验证,以及增加用户智能体验,能自动识别等等。从细节做起,让用户体验不一样的方便与智能。...

FCk富文本控件 及配置

  • 2010年05月19日 17:15
  • 1.84MB
  • 下载

JSP页面使用富文本控件ckeditor提交表单数据

ckeditor环境搭建 ,请参考  JSP中使用CKEditor3.6入门教程  JSP页面使用富文本控件ckeditor提交表单数据 JSP页面使用富文本控件ckeditor自定义样式 ...

ckeditor富文本控件

  • 2017年08月16日 10:05
  • 2.44MB
  • 下载

NSTextAttachment富文本控件实现图文混排

NSTextAttachment富文本控件实现图文混排

恢复文本控件的滚动功能

  • 2014年05月31日 12:25
  • 1.57MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:数字文本控件
举报原因:
原因补充:

(最多只允许输入30个字)