TypeConverter示例(一)

// 调控件的宽度属性
        [Bindable( true ), Category( " Appearance " ), DefaultValue( "" )]
        
override   public  Unit Width
        {
            
get
            {
                
return  tb.Width;
            }
            
set
            {
                tb.Width 
=  value;
            }
        }

        
private   int  _rows  =   5 ;
        
// 调控件的宽度属性
        [Bindable( true ), Category( " Appearance " ), DefaultValue( 1 )]
        
public   int  Rows
        {
            
get
            {
                
return  _rows;
            }
            
set
            {
                _rows 
=  value;
            }
        }

        
private   int  _cols  =   45 ;
        
// 调控件的宽度属性
        [Bindable( true ), Category( " Appearance " ), DefaultValue( 30 )]
        
public   int  Cols
        {
            
get
            {
                
return  _cols;
            }
            
set
            {
                _cols 
=  value;
            }
        }


        
private   int  _size  =   45 ;
        
// 调控件的宽度属性
        [Bindable( true ), Category( " Appearance " ), DefaultValue( 30 )]
        
public   int  Size
        {
            
get
            {
                
return  _size;
            }
            
set
            {
                _size 
=  value;
            }
        }


        
// 调控件的宽度属性
        [Bindable( true ), Category( " Appearance " ), DefaultValue( true )]
        
public   override   bool  Enabled
        {
            
get
            {
                
return  tb.Enabled;
            }
            
set
            {
                tb.Enabled 
=  value;
            }
        }


        
private   string  _maximumValue  =   null ;

        [Bindable(
true ), Category( " Appearance " ), DefaultValue( null )]
        
public   string  MaximumValue
        {
            
get
            {
                
return  _maximumValue;
            }
            
set
            {
                _maximumValue 
=  value;
            }
        }

        
private   string  _minimumValue  =   null ;
        [Bindable(
true ), Category( " Appearance " ), DefaultValue( null )]
        
public   string  MinimumValue
        {
            
get
            {
                
return  _minimumValue;
            }
            
set
            {
                _minimumValue 
=  value;
            }
        }




        
///   <summary>  
        
///  将此控件呈现给指定的输出参数。
        
///   </summary>
        
///   <param name="output">  要写出到的 HTML 编写器  </param>
         protected   override   void  Render(HtmlTextWriter output)
        {
            
// output.Write(Text);
             if  ( this .TextMode  ==   " MultiLine " )
            {
                output.WriteLine(
" <script type=\ " text / javascript\ " > " );
                output.WriteLine(
" function isMaxLen(o){ " );
                output.WriteLine(
" var nMaxLen=o.getAttribute? parseInt(o.getAttribute(\ " maxlength\ " )):\ " \ " ; " );
                output.WriteLine(
"  if(o.getAttribute && o.value.length>nMaxLen){ " );
                output.WriteLine(
"  o.value=o.value.substring(0,nMaxLen) " );
                output.WriteLine(
" }}</script> " );

                
this .AddAttributes( " rows " , Rows.ToString());
                
this .AddAttributes( " cols " , Cols.ToString());
            }
            
else
            {
                
if  ( this .Width.Value.ToString()  !=   "" )
                {
                    
if  (Width.Type.ToString()  !=   "" )
                    {
                        
this .AddAttributes( " SIZE " , (Width.Value  /   10 ).ToString()  +  (Width.Type.ToString()  ==   " ixel "   ?   " px "  :  " % " ));
                    }

                    
else
                    {
                        
this .AddAttributes( " SIZE " , (Width.Value  /   10 ).ToString()  +   " px " );
                    }
                }

                
if  ( this .Size.ToString()  !=   "" )
                {
                    
this .AddAttributes( " SIZE " this .Size.ToString());
                }
            }

            
// 当指定了输入框的最小或最大值时,则加入校验范围项
             if  ( this .MaximumValue  !=   null   ||   this .MinimumValue  !=   null )
            {
                rangevalidator.ControlToValidate 
=  tb.ID;
                rangevalidator.Type 
=  ValidationDataType.Double;
                rangevalidator.ForeColor 
=  System.Drawing.Color.Black;

                
if  ( this .MaximumValue  !=   null   &&   this .MinimumValue  !=   null )
                {
                    rangevalidator.MaximumValue 
=   this .MaximumValue;
                    rangevalidator.MinimumValue 
=   this .MinimumValue;
                    rangevalidator.ErrorMessage 
=   "  当前输入数据应在 "   +   this .MinimumValue  +   " "   +   this .MaximumValue  +   " 之间! " ;
                }
                
else
                {
                    
if  ( this .MaximumValue  !=   null )
                    {
                        rangevalidator.MaximumValue 
=   this .MaximumValue;
                        rangevalidator.ErrorMessage 
=   "  当前输入数据允许最大值为 "   +   this .MaximumValue;
                    }
                    
if  ( this .MinimumValue  !=   null )
                    {
                        rangevalidator.MinimumValue 
=   this .MinimumValue;
                        rangevalidator.ErrorMessage 
=   "  当前输入数据允许最小值为 "   +   this .MinimumValue;
                    }
                }
                rangevalidator.Display 
=  ValidatorDisplay.Static;
                
this .Controls.Add(rangevalidator);
            }


            RenderChildren(output);
        }


        
#region  IPostBackDataHandler 成员



        
public   void  RaisePostDataChangedEvent()
        { }


        
public   bool  LoadPostData( string  postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            
string  presentValue  =   this .tb.Text;
            
string  postedValue  =  postCollection[postDataKey];

            
if  ( ! presentValue.Equals(postedValue)) // 如果回发数据不等于原有数据
            {
                
this .Text  =  postedValue;
                
this .tb.Text  =  postedValue;
                
return   true ;
            }
            
return   false ;

        }
        
#endregion

    }

    
public   class  RequiredFieldTypeControlsConverter : StringConverter
    {
        
public  RequiredFieldTypeControlsConverter() { }

        
public   override   bool  GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            
return   true ;
        }
        
public   override  System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            ArrayList controlsArray 
=   new  ArrayList();
            controlsArray.Add(
" 暂无校验 " );
            controlsArray.Add(
" 数据校验 " );
            controlsArray.Add(
" 电子邮箱 " );
            controlsArray.Add(
" 移动手机 " );
            controlsArray.Add(
" 家用电话 " );
            controlsArray.Add(
" 身份证号码 " );
            controlsArray.Add(
" 网页地址 " );
            controlsArray.Add(
" 日期 " );
            controlsArray.Add(
" 日期时间 " );
            controlsArray.Add(
" 金额 " );
            controlsArray.Add(
" IP地址 " );
            controlsArray.Add(
" IP地址带端口 " );
            
return   new  StandardValuesCollection(controlsArray);

        }
        
public   override   bool  GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            
return   false ;
        }
    }


    
public   class  CanBeNullControlsConverter : StringConverter
    {
        
public  CanBeNullControlsConverter() { }

        
public   override   bool  GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            
return   true ;
        }

        
public   override  System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            ArrayList controlsArray 
=   new  ArrayList();
            controlsArray.Add(
" 可为空 " );
            controlsArray.Add(
" 必填 " );

            
return   new  StandardValuesCollection(controlsArray);

        }

        
public   override   bool  GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            
return   false ;
        }
    }


    
public   class  FormControlsConverter : StringConverter
    {
        
public  FormControlsConverter()
        {
        }

        
public   override   bool  GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            
return   true ;
        }

        
public   override  System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            ControlCollection Controls 
=  ((Page)context.Container.Components[ 0 ]).Controls;
            ArrayList controlsArray 
=   new  ArrayList();
            
for  ( int  i  =   0 ; i  <  Controls.Count; i ++ )
            {
                
if  ((Controls[i]  is  HtmlTable
                    
||  Controls[i]  is  HtmlForm
                    
||  Controls[i]  is  HtmlGenericControl
                    
||  Controls[i]  is  HtmlImage
                    
||  Controls[i]  is  Label
                    
||  Controls[i]  is  DataGrid
                    
||  Controls[i]  is  DataList
                    
||  Controls[i]  is  Table
                    
||  Controls[i]  is  Repeater
                    
||  Controls[i]  is  Image
                    
||  Controls[i]  is  Panel
                    
||  Controls[i]  is  PlaceHolder
                    
||  Controls[i]  is  Calendar
                    
||  Controls[i]  is  AdRotator
                    
||  Controls[i]  is  Xml
                    ))
                {
                    controlsArray.Add(Controls[i].ClientID);
                }
            }
            
return   new  StandardValuesCollection(controlsArray);

        }

        
public   override   bool  GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            
return   false ;
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值