服务器控件开发之一:数值型控件

来源:互联网 发布:网络报销管理系统 编辑:程序博客网 时间:2024/05/06 06:36
数值型控件是比较麻烦的事,经常看到大家在论谈里,讲要求输入数值型控件。问了N遍不得结果,怎么做出来都不太满意,其实真做好还是比较难的。我想将我写的一个让大家参考参考,并帮助测试。
控件名称: NumericTextBox 数据类型: float型,正float型,负float型,正数,正整数,负正数(除此之外,不知道还有没有其他类型有欢迎大家提出来,加上去)。 功能 除支持上述六种类型外,还支持粘贴数值型数据,非数值型粘贴不进去。
具体代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomAjaxToolKit
{
    [DefaultProperty(
"Text")]
    [ToolboxData(
"<{0}:NumericTextBox runat=server></{0}:NumericTextBox>")]
    
public class NumericTextBox : TextBox
    
{
        
private string strInteger = @"^([+-]?)/d+$";   //整数
        private string strPlusInteger = @"^([+]?)/d+$";//正整数
        private string strNegativeInteger = @"^-/d+$"//负整数

        
private string strDecimal = @"^([+-]?)/d*/.?/d+$";   //小数(含正负)
        private string strPlusDecimal = @"^([+]?)/d*/.?/d+$";//正小数
        private string strNegativeDecimal = @"^-/d*/.?/d+$"//负小数

        
private string strHeader = "<script language="javascript" type="text/javascript"> <!--  ";
        
private string strFooter = "-->  </script>";
        
//限制输入字符清单:
            
//keycode 48-57表示0-9
            
//keycode 107  表示+
            
//keycode 45   表示-
            
//keycode 46   表示小数点

        
//如下表达式表示输入的可以是正负float类型数据
        
//if(!((keycode>47&&keycode<58)||keycode==107||keycode==45||keycode==46))  
        private string strAllDecKeyPressExpr = "  if(!((keycode>47&&keycode<58)||keycode==107||keycode==45||keycode==46))  ";

        
//如下表达式表示输入的可以是正float类型数据
        
//if(!((keycode>47&&keycode<58)||keycode==107||keycode==46))  
        private string strPlusDecKeyPressExpr = "  if(!((keycode>47&&keycode<58)||keycode==107||keycode==46))  ";

        
//如下表达式表示输入的可以是负float类型数据
        
//if(!((keycode>47&&keycode<58)||keycode==45||keycode==46))  
        private string strNegativeDecKeyPressExpr = "  if(!((keycode>47&&keycode<58)||keycode==45||keycode==46))  ";

        
//如下表达式表示输入的可以是正负整数类型数据
        
//if(!((keycode>47&&keycode<58)||keycode==107||keycode==45))  
        private string strAllIntKeyPressExpr = "  if(!((keycode>47&&keycode<58)||keycode==107||keycode==45))  ";

        
//如下表达式表示输入的可以是正整数类型数据
        
//if(!((keycode>47&&keycode<58)||keycode==107))  
        private string strPlusIntKeyPressExpr = "  if(!((keycode>47&&keycode<58)||keycode==107))  ";

        
//如下表达式表示输入的可以是负整数类型数据
        
//if(!((keycode>47&&keycode<58)||keycode==45))  
        private string strNegativeIntKeyPressExpr = "  if(!((keycode>47&&keycode<58)||keycode==45))  ";

        
//检查“-”号的合法性,如果不合法,则清除输入的不合法“-”号
        private string strNavDecIntValidateValue = "  str=ctrl.value; " +
                                                 
"  var nNumerics= str.split("-");    " +
                                                 
"  if(str.substring(0,1)!="-") " +
                                                 
"  { " +
                                                 
"      if(nNumerics.length==2) " +
                                                 
"      { " +
                                                 
"           ctrl.value=str.substring(0,str.length-1); " +
                                                 
"      } " +
                                                 
"      return; " +
                                                 
"  } " +
                                                 
"  else if(str.substring(0,1)=="-") " +
                                                 
"  { " +
                                                 
"      if(nNumerics.length==3) " +
                                                 
"      { " +
                                                 
"          ctrl.value=str.substring(0,1)+str.substring(1,str.length-1); " +
                                                 
"      } " +
                                                 
"  }  ";

        
//检查“+”号的合法性,如果不合法,则清除输入的不合法“+”号
        private string strPlusDecIntValidateValue = "  str=ctrl.value; " +
                                                 
"  var pNumerics= str.split("+");    " +
                                                 
"  if(str.substring(0,1)!="+") " +
                                                 
"  { " +
                                                 
"      if(pNumerics.length==2) " +
                                                 
"      { " +
                                                 
"           ctrl.value=str.substring(0,str.length-1); " +
                                                 
"      } " +
                                                 
"      return; " +
                                                 
"  } " +
                                                 
"  else if(str.substring(0,1)=="+") " +
                                                 
"  { " +
                                                 
"      if(pNumerics.length==3) " +
                                                 
"      { " +
                                                 
"          ctrl.value=str.substring(0,1)+str.substring(1,str.length-1); " +
                                                 
"      } " +
                                                 
"  }  ";


        [DefaultValue(
2)]
        [Description(
"设置小数位位数,如:2位,设置为2,数字123.23")]
        
public int Digital
        
{
            
get
            
{
                
object o = (object)ViewState["Digital"];
                
return (o == null? 0 : (int)o;
            }

            
set
            
{
                ViewState[
"Digital"= value;
            }

        }


        [DefaultValue(NumericType.Decimal)]
        [Description(
"数据类型六种:Decimal-小数(含正负),PlusDecimal-正小数,NegativeDecimal-负小数;Integer-整数(含正负),PlusInteger-正整数,NegativeInteger-负整数")]
        
public NumericType NumType
        
{
            
get 
            
{
                
object o = (object)ViewState["NumType"];
                
return (o == null? NumericType.Decimal : (NumericType)o;
            }

            
set
            
{
                ViewState[
"NumType"= value;
            }

        }


        
//复写AddAttributesToRender方法,将相关客户端的操作事件写进控件属性
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        
{
            
this.Attributes.Add("onkeypress""NumericTextBox_KeyPress()");
            
this.Attributes.Add("onkeyup""NumericTextBox_KeyUp(this)");
            
this.Attributes.Add("onpaste""return NumericTextBox_Paste()");
            
base.AddAttributesToRender(writer);
        }


        
//将控件的注册业务逻辑
        protected override void OnPreRender(EventArgs e)
        
{
            
string strScript = strHeader;
            strScript 
= strScript + "function NumericTextBox_KeyPress()   ";
            strScript 
= strScript + "";
            strScript 
= strScript + "  var keycode=event.keyCode;  ";

            
switch (NumType)
            

                
case NumericType.Decimal:
                    strScript 
= strScript + strAllDecKeyPressExpr;
                    
break;
                
case NumericType.PlusDecimal:
                    strScript 
= strScript + strPlusDecKeyPressExpr;
                    
break;
                
case NumericType.NegativeDecimal:
                    strScript 
= strScript + strNegativeDecKeyPressExpr;
                    
break;
                
case NumericType.Integer:
                    strScript 
= strScript + strAllIntKeyPressExpr;
                    
break;
                
case NumericType.PlusInteger:
                    strScript 
= strScript + strPlusIntKeyPressExpr;
                    
break;
                
case NumericType.NegativeInteger:
                    strScript 
= strScript + strNegativeIntKeyPressExpr;
                    
break;
            }


            strScript 
= strScript + "  {    ";
            strScript 
= strScript + "      event.keyCode= 0;  ";
            strScript 
= strScript + "  }    ";
            strScript 
= strScript + "";
            strScript 
= strScript + "function NumericTextBox_KeyUp(ctrl) ";
            strScript 
= strScript + "";
            
if ((NumType == NumericType.Decimal) || (NumType == NumericType.PlusDecimal) || (NumType == NumericType.NegativeDecimal))
            
{
                strScript 
= strScript + "  var str= ctrl.value;  ";
                strScript 
= strScript + "  var numerics= str.split(".");  ";
                strScript 
= strScript + "  if(numerics.length==3)  ";
                strScript 
= strScript + "  {   ";
                strScript 
= strScript + "       ctrl.value=str.substring(0,str.length-1); ";
                strScript 
= strScript + "  }  ";
            }


            
//设置小数位
            if (NumType == NumericType.Decimal || NumType == NumericType.PlusDecimal || NumType == NumericType.NegativeDecimal)
            
{
                strScript 
= strScript +"  str=ctrl.value; ";
                strScript 
= strScript +"  var digtPos=str.indexOf("."); ";
                strScript 
= strScript + " if((digtPos>=0)&&(str.length>digtPos+3)) ";
                strScript 
= strScript + " { ";
                strScript 
= strScript + "        ctrl.value=str.substring(0,digtPos+1+" + Digital.ToString().Trim() + "); ";
                strScript 
= strScript + " } ";
            }


            
//设置小数位

            
//负数清除
            if (NumType == NumericType.NegativeDecimal || NumType == NumericType.NegativeInteger)
            
{
                strScript 
= strScript + "  str=ctrl.value; ";
                strScript 
= strScript + "  if(str.substring(0,1)!="-") ";
                strScript 
= strScript + "  { ";
                strScript 
= strScript + "        ctrl.value=""; ";
                strScript 
= strScript + "        return; ";
                strScript 
= strScript + "  } ";
            }

            
//负数清除
            
//if ((NumType == NumericType.Decimal) || (NumType == NumericType.Integer))
            
//{
            strScript = strScript + strNavDecIntValidateValue;  //清除非法"-"
            strScript = strScript + strPlusDecIntValidateValue; //清除非法"+"
            
//}

            strScript 
= strScript + "";
            strScript 
= strScript + "function NumericTextBox_Paste()   ";
            strScript 
= strScript + "";
            strScript 
= strScript + "  var b=clipboardData.getData('text');  ";

            
switch (NumType)
            

                
case NumericType.Decimal:
                    strScript 
= strScript + "  var c=b.match("" + strDecimal + "");  ";
                    
break;
                
case NumericType.PlusDecimal:
                    strScript 
= strScript + "  var c=b.match("" + strPlusDecimal + "");  ";
                    
break;
                
case NumericType.NegativeDecimal:
                    strScript 
= strScript + "  var c=b.match("" + strNegativeDecimal + "");  ";
                    
break;
                
case NumericType.Integer:
                    strScript 
= strScript + "  var c=b.match("" + strInteger + "");  ";
                    
break;
                
case NumericType.PlusInteger:
                    strScript 
= strScript + "  var c=b.match("" + strPlusInteger + "");  ";
                    
break;
                
case NumericType.NegativeInteger:
                    strScript 
= strScript + "  var c=b.match("" + strNegativeInteger + "");  ";
                    
break;
            }


            strScript 
= strScript + "return (c==null||c==false)?false:true;  ";
            strScript 
= strScript + "";

            strScript 
= strScript + strFooter;
            
if (!this.Page.ClientScript.IsStartupScriptRegistered("NumericScript"))
                
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "NumericScript", strScript);
            
base.OnPreRender(e);
        }


    }


    
public enum NumericType
    
{
        Decimal,        
//小数
        PlusDecimal,    //正小数
        NegativeDecimal,//负小数
        Integer,        //数数
        PlusInteger,    //正整数
        NegativeInteger //正整数
    }

}


控件缺点:
1、当输入非法字符时,可以看到相差字符闪一下,感觉不太自然,但能保证数据正确。
2、当页面提交前进行检测的话,注册客户端脚本没法通用。如果要通用,可以写成类的形式,然后进行注册客户端脚本。欢迎大家提意见!!!!!!
3、为了简单起见而用复写TextBox控件,如果大家有兴趣,则可以继承自WebControl,这样写出来不知道效果怎样,也许可能会好点,但差不多。

些控件版权归原作者所有,引用需要得到许可。
特别强调:此控件只可作为学习和研究之用,不可作为商业用途,违者必究!!!!!!
原创粉丝点击