WebContorl示例--NumberTextBox

代码
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Text;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;

[assembly: WebResource(
" WebUIControl.Js.NumberTextBox.js " " application/x-javascript " )]
namespace  WebUIControl
{
    [DefaultProperty(
" Text " )]
    [ToolboxData(
" <{0}:NumberTextBox runat=server></{0}:NumberTextBox> " )]
    
public   class  NumberTextBox : CompositeControl
    {
        
private  TextBox _textBox;
        
private  HiddenField _hiddenField;

        [Bindable(
true )]
        [Category(
" Appearance " )]
        [DefaultValue(
true )]
        [Localizable(
true )]
        
public   bool  IsUseSeparator
        {
            
get
            {
                
object  obj  =   this .ViewState[ " IsUseSeparator " ];
                
if  (obj  !=   null )
                {
                    
return  ( bool )obj;
                }
                
return   true ;
            }
            
set
            {
                
this .ViewState[ " IsUseSeparator " =  value;
            }
        }

        [Bindable(
true )]
        [Category(
" Appearance " )]
        [DefaultValue(
null )]
        [Localizable(
true )]
        
public   decimal ?  MinValue
        {
            
get
            {
                
object  obj  =   this .ViewState[ " MinValue " ];
                
if  (obj  !=   null )
                {
                    
return  ( decimal ? )obj;
                }
                
return   null ;
            }
            
set
            {
                
this .ViewState[ " MinValue " =  value;
            }
        }

        [Bindable(
true )]
        [Category(
" Appearance " )]
        [DefaultValue(
null )]
        [Localizable(
true )]
        
public   decimal ?  MaxValue
        {
            
get
            {
                
object  obj  =   this .ViewState[ " MaxValue " ];
                
if  (obj  !=   null )
                {
                    
return  ( decimal ? )obj;
                }
                
return   null ;
            }
            
set
            {
                
this .ViewState[ " MaxValue " =  value;
            }
        }

        [Browsable(
false )]
        [Themeable(
false )]
        
public   decimal ?  EditValue
        {
            
get
            {
                
object  obj  =   this .ViewState[ " EditValue " ];
                
if  (obj  !=   null )
                {
                    
return  ( decimal ? )obj;
                }
                
return   null ;
            }
            
set
            {
                
this .EnsureChildControls();
                
this .ViewState[ " EditValue " =  value;
                
if  (value  ==   null )
                {
                    
this ._textBox.Text  =   string .Empty;
                }
                
else
                {
                    
string  format  =   " 0 " ;
                    
if  ( this .DecimalDigits  !=   null   &&   this .DecimalDigits.Value  >   0 )
                    {
                        format 
+=   " . " ;
                        
for  ( int  i  =   0 ; i  <   this .DecimalDigits.Value; i ++ )
                        {
                            format 
+=   " 0 " ;
                        }
                    }
                    
if  ( this .IsUseSeparator)
                    {
                        format 
=   " #,## "   +  format;
                    }

                    
this ._textBox.Text  =  value.Value.ToString(format);
                }
            }
        }

        
///   <summary>
        
///  返回显示值的文本框
        
///   </summary>
        [Browsable( false )]
        
public  TextBox TextBox
        {
            
get
            {
                EnsureChildControls();
                
return  _textBox;
            }
        }

        [Bindable(
true )]
        [Category(
" Appearance " )]
        [DefaultValue(
null )]
        [Localizable(
true )]
        
public   uint ?  DecimalDigits
        {
            
get
            {
                
object  obj  =   this .ViewState[ " DecimalDigits " ];
                
if  (obj  !=   null )
                {
                    
return   Convert.ToUInt16(obj);
                }
                
return   null ;
            }
            
set
            {
                
this .EnsureChildControls();
                
this .ViewState[ " DecimalDigits " =  value;
                
this ._hiddenField.Value  =  value.ToString();
            }
        }



        [Bindable(
true )]
        [Category(
" Appearance " )]
        [DefaultValue(
"" )]
        [Localizable(
true )]
        
public   override   string  CssClass
        {
            
get
            {
                
object  obj  =   this .ViewState[ " TextCssClass " ];
                
if  (obj  !=   null )
                {
                    
return  ( string )obj;
                }
                
return   string .Empty;
            }
            
set
            {
                
this .EnsureChildControls();
                
this .ViewState[ " TextCssClass " =  value;
                
this ._textBox.CssClass  =  value;
            }
        }


        
protected   override   void  OnInit(EventArgs e)
        {
            
base .OnInit(e);
            
if  ( ! Page.ClientScript.IsClientScriptIncludeRegistered(Page.GetType(),  " NumberTextBox " ))
            {
                
string  webUrl  =  Page.ClientScript.GetWebResourceUrl(GetType(),  " WebUIControl.Js.NumberTextBox.js " );
                Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), 
" NumberTextBox " , webUrl);
            }
        }

        
protected   override   void  CreateChildControls()
        {
            
this .Controls.Clear();
            
this ._textBox  =   new  TextBox();
            
this ._hiddenField  =   new  HiddenField();
            
this .Controls.Add( this ._textBox);
            
this .Controls.Add( this ._hiddenField);
            
this .ClearChildViewState();
        }

        
protected   override   void  OnLoad(EventArgs e)
        {
            
base .OnLoad(e);

            
this .EnsureChildControls();
            
this ._textBox.Attributes.Add( " style " " ime-mode:disabled " );
            
string  min  =   this .MinValue  ==   null   ?   ""  :  this .MinValue.Value.ToString();
            
string  max  =   this .MaxValue  ==   null   ?   ""  :  this .MaxValue.Value.ToString();
            
this ._textBox.Attributes.Add( " onblur " " return PL_NumberTextBox_onblur(' "   +   this .ClientID  +   " '); " );
            
this ._textBox.Attributes.Add( " onfocusin " " return PL_NumberTextBox_onfocus(' "   +   this .ClientID  +   " '); " );
            
if  ( this .MinValue  !=   null )
            {
                
this ._textBox.Attributes.Add( " minValue " this .MinValue.Value.ToString());
            }
            
if  ( this .MaxValue  !=   null )
            {
                
this ._textBox.Attributes.Add( " maxValue " this .MaxValue.Value.ToString());
            }
            
if  ( ! this .IsUseSeparator)
            {
                
this ._textBox.Attributes.Add( " separator " " false " );
            }
        }

        
protected   override   void  LoadViewState( object  savedState)
        {
            
base .LoadViewState(savedState);
            EnsureChildControls();
            
this .ViewState[ " TextCssClass " =   this ._textBox.CssClass;
            
this .ViewState[ " DecimalDigits " =   this ._hiddenField.Value;
            
this .ViewState[ " EditValue " =   this ._textBox.Text.Trim().Replace( " , " "" );
        }

    }
}

 

 

 

 

代码

function  PL_NumberTextBox_onfocus(controlid)
{
    
var  numberValue  =  PL_NumberTextBox_getValue(controlid);
    
var  textbox  =  document.getElementById(controlid).firstChild;
    textbox.value 
=  numberValue;
    
return   false ;
}

function  PL_NumberTextBox_onblur(controlid)
{
    
var  numberValue  =  PL_NumberTextBox_getValue(controlid);
    
var  result  =  PL_NumberTextBox_check(numberValue,controlid);
    
if (result)
    {
        PL_NumberTextBox_setValue(numberValue,controlid);
    }
    
else
    {
        
var  textbox  =  document.getElementById(controlid).firstChild;
        textbox.focus();
    }
    
return   false ;
}

function  PL_NumberTextBox_getValue(controlid)
{
    
var  textbox = null ;
    
if ( typeof (controlid) == ' string ' )
        textbox 
=  document.getElementById(controlid).firstChild;
        
else  textbox = controlid.firstChild;
    
var  numberValue  =  textbox.value;
    
// 去除前后空格
     var  trimnum;
    trimnum 
=  numberValue.replace( / (^\s*)|(\s*$) / g,  "" );
    
// 去除千分符
     var  result;
    result 
=  trimnum.replace( / , / g,  "" );
    
return  result;
}


//获得控件的值,controlid为clientID或control
function  PL_NumberTextBox_GetControlValue(controlid)
{
    
var  control = controlid;
    
if ( typeof (controlid) == ' string ' )
        control
= document.getElementById(controlid);
    
if (control == null )
        
return   null ;
    
var  textValue = PL_NumberTextBox_getValue(control);
    
if (textValue == null || textValue == "" )
    
return   null ;
    
var  result =  Number(textValue);
    
return  result;
}

// 提供客户端设置数字值的方法
function  PL_NumberTextBox_setValue(numberValue,controlid)
{
    
if (document.getElementById(controlid) == null )
    
return ;
    
var  textbox  =  document.getElementById(controlid).firstChild;
    
var  hiddenfield  =  document.getElementById(controlid).lastChild;
    
var  digits  =  hiddenfield.value;
    
var  separator;
    
if (textbox.separator  ==  undefined) separator  =   true ;
    
else  separator  =   false ;
    
var  textboxValue  =  PL_NumberTextBox_formatValue(numberValue,separator,digits, true );
    textbox.value 
=  textboxValue;
}

// 提供客户端改变小数位数的方法,该方法设置HiddenField中的值,并重新格式化数字
function  PL_NumberTextBox_setDigits(digits,controlid)
{
    
if (document.getElementById(controlid) == null )
        
return ;
    
var  hiddenfield  =  document.getElementById(controlid).lastChild;
    hiddenfield.value 
=  digits;
    
    
var  numberValue  =  PL_NumberTextBox_getValue(controlid);
    PL_NumberTextBox_setValue(numberValue,controlid);
}

// 判断数字格式合法性
function  PL_NumberTextBox_check(numberValue,controlid)
{
    
if (numberValue  ==   "" )
    {
        
return   true ;
    }
    
// 验证是否是数字
     var  vv;
    vv 
=  isNaN(numberValue);
    
if (vv)
    {
        alert(
" Can only enter numbers. " );
        
return   false ;
    }
    
else
    {
        
var  numbervalue  =  Number(numberValue);
        
var  textbox  =  document.getElementById(controlid).firstChild;
        
if (textbox.minValue  !=  undefined)
        {
            
if (numbervalue  <  Number(textbox.minValue))
            {
                alert(
" This number can only be between  "   +  textbox.minValue  + "  and  "   +  textbox.maxValue  +   " . " );
                
return   false ;
            }
        }
        
if (textbox.maxValue  !=  undefined)
        {
            
if (numbervalue  >  Number(textbox.maxValue))
            {
                alert(
" This number can only be between  "   +  textbox.minValue  + "  and  "   +  textbox.maxValue  +   " . " );
                
return   false ;
            }
        }
        
        
return   true ;
    }
}

function  PL_NumberTextBox_formatValue(fnumber,fdivide,fpoint,fround)
{
    
if (fnumber == null || fnumber == '' )
    {
    
return   "" ;
    }
    
var  fnum  =  fnumber  +   '' ;
    
var  revalue = "" ;

    
var  hasF  =   " 0 " ;
    
if (fnum == null )
    {
        
for ( var  i = 0 ;i < fpoint;i ++ )revalue += " 0 " ;
        
return   " 0 " ;
    }
    fnum 
=  fnum.replace( / ^\s*|\s*$ / g, '' );
    
if (fnum == "" )
    {
        
for ( var  i = 0 ;i < fpoint;i ++ )revalue += " 0 " ;
        
return   " 0 " ;
    }

    fnum
= fnum.replace( / , / g, "" );

    
if (fnum.indexOf( ' - ' ) == 0 )
    {   
        hasF 
=   " 1 " ;
        fnum 
=  fnum.substring( 1 ,fnum.length);
    }
    
if (fround)
    {
        
var  temp  =   " 0. " ;
        
for ( var  i = 0 ;i < fpoint;i ++ )temp += " 0 " ;
        temp 
+=   " 5 " ;

        fnum 
=  Number(fnum)  +  Number(temp);
        fnum 
+=   '' ;
    }

    
var  arrayf = fnum.split( " . " );

    
if (fdivide)
    { 
        
if (arrayf[ 0 ].length > 3 )
        {
            
while (arrayf[ 0 ].length > 3 )
            {
                revalue
= " , " + arrayf[ 0 ].substring(arrayf[ 0 ].length - 3 ,arrayf[ 0 ].length) + revalue;
                arrayf[
0 ] = arrayf[ 0 ].substring( 0 ,arrayf[ 0 ].length - 3 );
            }
        }
    }
    revalue
= arrayf[ 0 ] + revalue;

    
if (arrayf.length == 2 && fpoint != 0 )
    {
        arrayf[
1 ] = arrayf[ 1 ].substring( 0 ,(arrayf[ 1 ].length <= fpoint) ? arrayf[ 1 ].length:fpoint);

        
if (arrayf[ 1 ].length < fpoint)
        
for ( var  i = 0 ;i < fpoint - arrayf[ 1 ].length;i ++ )arrayf[ 1 ] += " 0 " ;
        revalue
+= " . " + arrayf[ 1 ];
    }
    
else   if (arrayf.length == 1 && fpoint != 0 )
    {
        revalue
+= " . " ;
        
for ( var  i = 0 ;i < fpoint;i ++ )revalue += " 0 " ;
    }
    
if (hasF == " 1 " )
    {
        
return  revalue  =   " - " + revalue;
    }
    
else
    {
        
return  revalue;
    }
}

 

 

你可能感兴趣的:(number)