卧龙居浩泽

To be a better one to satisfy you…

郭浩泽ID:Haoze
1346次访问,排名2万外好友1人,关注者8
软件开发;程序员;中山大学
Haoze的文章
原创 8 篇
翻译 0 篇
转载 0 篇
评论 0 篇
最近评论
文章分类
收藏
    相册
    常用文档
    Spring.Net 文档
    大师
    谭振林
    存档
    订阅我的博客
    XML聚合  FeedSky

    原创  自定义服务器控件系列(5)——自由的注册验证控件(RangValidator的Adapter + 修改以往的结构)收藏

    新一篇: 自定义服务器控件系列(6)——控件客户端属性也扩展(我不想要CascadeSelect的value,我要Text) | 旧一篇: 自定义服务器控件系列(4)——自由的注册验证控件(出大问题啦!)

    社区那边的开发人员告诉我,他们需要每次为TextBox加上验证控件。最大的表单有49个控件,大部分时间用在加验证控件上,既无技术含量,又容易出错。可不可以出一种带Type的TextBox呢?
    听起来很美好。就开始做吧。

    经过和他们一段时间的探讨需求,确定ZTypeTextBox的Feature list如下:
    1> 有Type属性,支持Double,Integer,Date,String
    2> 有Required属性,以支持必填或允许空。
    3>有MaximumValue,MinimumValue属性。
    4>有RequiredErrorMessage何RangeErrorMessage属性,可以自定义出错信息。
    真是想要什么就有什么。刚刚才研究了几个验证控件,就可以直接使用了。

    分析一下需求,很明显,我们需要一个ClientRangeValidatorAdapter。反编译一下这个验证控件,原来和CompareValidator同样是继承自BaseCompareValidator,那么类的层次就要修改了。经过修改的两个类,和新加的一个类如下:(现在是为公司工作,就要换上公司的命名空间啦!)

    // ClientBaseCompareValidatorAdapter 

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI.WebControls;
    using System.Globalization;
    using System.Web.UI;

    namespace ZSoft.Platform.Web.UI.Controls
    {
        
    public abstract class ClientBaseCompareValidatorAdapter : ClientBaseValidatorAdapter
        
    {
            
    protected ClientBaseCompareValidatorAdapter(BaseCompareValidator validator, WebControl parentControl)
                : 
    base(validator, parentControl)
            
    {
            }


            
    private BaseCompareValidator GetValidator()
            
    {
                
    return m_Validator as BaseCompareValidator;
            }



            
    public override void AddAttributesToRender()
            
    {
                
    base.AddAttributesToRender();

                
    if (GetValidator().Type != ValidationDataType.String)
                
    {
                    m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                          
    "type", PropertyConverter.EnumToString(typeof(ValidationDataType), GetValidator().Type), false);
                }


                NumberFormatInfo currentInfo 
    = NumberFormatInfo.CurrentInfo;
                
    switch (GetValidator().Type)
                
    {
                    
    case ValidationDataType.Double:
                        
    {
                            
    string numberDecimalSeparator = currentInfo.NumberDecimalSeparator;
                            m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                                
    "decimalchar", numberDecimalSeparator);
                            
    return;
                        }

                    
    case ValidationDataType.Currency:
                        
    {
                            
    string currencyDecimalSeparator = currentInfo.CurrencyDecimalSeparator;
                            m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                                
    "decimalchar", currencyDecimalSeparator);
                            
    string currencyGroupSeparator = currentInfo.CurrencyGroupSeparator;
                            
    if (currencyGroupSeparator[0== 'a0')
                            
    {
                                currencyGroupSeparator 
    = " ";
                            }

                            m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                                
    "groupchar", currencyGroupSeparator);
                            m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                                
    "digits", currentInfo.CurrencyDecimalDigits.ToString(NumberFormatInfo.InvariantInfo), false);
                            
    int currencyGroupSize = GetCurrencyGroupSize(currentInfo);
                            
    if (currencyGroupSize > 0)
                            
    {
                                m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                                    
    "groupsize", currencyGroupSize.ToString(NumberFormatInfo.InvariantInfo), false);
                            }

                            
    break;
                        }

                    
    case ValidationDataType.Date:
                        
    {
                            m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                                
    "dateorder", GetDateElementOrder(), false);
                            m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                                
    "cutoffyear", CutoffYear.ToString(NumberFormatInfo.InvariantInfo), false);
                            
    int year = DateTime.Today.Year;
                            m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                                
    "century", (year - (year % 100)).ToString(NumberFormatInfo.InvariantInfo), false);
                            
    break;
                        }

                }

            }


            
    private methods for support
        }

    }


    //
    // 经过修改的CompareValidator类。
    //
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI.WebControls;
    using System.Globalization;
    using System.Web.UI;

    namespace ZSoft.Platform.Web.UI.Controls
    {
        
    //
        
    // 这是针对CompareValidator的具体实现。
        
    //
        public sealed class ClientCompareValidatorAdapter : ClientBaseCompareValidatorAdapter
        
    {
            
    public ClientCompareValidatorAdapter(CompareValidator validator, WebControl parentControl)
                : 
    base( validator, parentControl )
            
    {
            }


            
    private CompareValidator GetValidator()
            
    {
                
    return m_Validator as CompareValidator;
            }


            
    public override void AddAttributesToRender()
            
    {
                
    base.AddAttributesToRender();            

                m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                    
    "evaluationfunction""CompareValidatorEvaluateIsValid"false);

                
    if (GetValidator().ControlToCompare.Length > 0)
                
    {
                    
    string controlRenderID = GetControlRenderID(GetValidator().ControlToCompare);
                    m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                        
    "controltocompare", controlRenderID);
                    m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                         
    "controlhookup", controlRenderID);
                }


                
    if (GetValidator().ValueToCompare.Length > 0)
                
    {
                    m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                        
    "valuetocompare", GetValidator().ValueToCompare);
                }

                
    if (GetValidator().Operator != ValidationCompareOperator.Equal)
                
    {
                    m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
                        
    "operator", PropertyConverter.EnumToString(typeof(ValidationCompareOperator), GetValidator().Operator), false);
                }

            }


         
        }

    }


    //
    // 新增加的ClientRangeValidatorAdapter类
    //
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI.WebControls;

    namespace ZSoft.Platform.Web.UI.Controls
    {
        
    public sealed class ClientRangeValidatorAdapter : ClientBaseCompareValidatorAdapter
        
    {
            
    public ClientRangeValidatorAdapter(RangeValidator validator, WebControl parentControl)
                : 
    base( validator, parentControl )
            
    {
            }


            
    private RangeValidator GetValidator()
            
    {
                
    return m_Validator as RangeValidator;
            }


            
    public override void AddAttributesToRender()
            
    {
                
    base.AddAttributesToRender();
                
                m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(m_Validator.ClientID, 
                    
    "evaluationfunction""RangeValidatorEvaluateIsValid"false);
                
                m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(m_Validator.ClientID, 
                    
    "maximumvalue", GetValidator().MaximumValue);

                m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(m_Validator.ClientID, 
                    
    "minimumvalue", GetValidator().MinimumValue);

            }

        }

    }


    下一章,我们来实现ZTypeTextBox!

    发表于 @ 2008年05月21日 19:06:53|评论(loading...)|编辑

    新一篇: 自定义服务器控件系列(6)——控件客户端属性也扩展(我不想要CascadeSelect的value,我要Text) | 旧一篇: 自定义服务器控件系列(4)——自由的注册验证控件(出大问题啦!)

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © Haoze