Esempio n. 1
0
        public ClientVdtData GetPropertyValidator()
        {
            ClientVdtData cdtData = new ClientVdtData();

            cdtData.IsValidateOnSubmit  = true;
            cdtData.ValidateProp        = "value";
            cdtData.ClientIsHtmlElement = true;

            switch (this.DataType)
            {
            case PropertyDataType.Integer:
                cdtData.IsOnlyNum = true;
                break;

            case PropertyDataType.Decimal:
                cdtData.IsFloat = true;
                break;
            }

            foreach (PropertyValidatorDescriptor propValidator in this.Validators)
            {
                Validator vali = propValidator.GetValidator();
                if (vali is IClientValidatable)
                {
                    ClientVdtAttribute cvAttr = propValidator.ToClientVdtAttribute();

                    cdtData.CvtList.Add(cvAttr);

                    if (string.IsNullOrEmpty(cvAttr.ClientValidateMethodName) == false)
                    {
                        cvAttr.AdditionalData = ((IClientValidatable)vali).GetClientValidateAdditionalData(vali.Tag);
                    }
                }
            }

            return(cdtData);
        }
		/// <summary>
		/// 数据客户端验证初始化
		/// </summary>
		private void MappingDataToValidate()
		{
			foreach (DataBindingItem item in this.ItemBindings)
			{
				ClientVdtData cdtData = new ClientVdtData();
				cdtData.IsValidateOnSubmit = this.IsValidateOnSubmit;
				cdtData.FormatString = item.Format;
				cdtData.ValidationGroup = item.ValidationGroup;
				cdtData.ClientIsHtmlElement = item.ClientIsHtmlElement;

				Control vdtControl = this.FindControlByPath(this, item.ControlID);

				if (!this.CheckDataBingdingItem(item, vdtControl))
					continue;

				PropertyInfo propertyinfo = this.data.GetType().GetProperty(item.DataPropertyName);
				//徐磊修改,2011年建军节。。。
				if (propertyinfo == null && this.IsValidateChildren)
				{
					string[] splitstr = item.DataPropertyName.Split('.');
					Type t = this.Data.GetType();

					for (int i = 0; i < splitstr.Length; i++)
					{
						propertyinfo = t.GetProperty(splitstr[i]);
						if (propertyinfo != null)
						{
							t = propertyinfo.PropertyType;
						}
					}
				}

				if (propertyinfo == null)
					continue;

				object[] vobjects = propertyinfo.GetCustomAttributes(typeof(ValidatorCompositionAttribute), true);
				cdtData.ControlID = vdtControl.ClientID;

				if (vobjects.Length > 0)
				{
					cdtData.IsAnd =
					((ValidatorCompositionAttribute)vobjects[0]).CompositionType == CompositionType.And ? true : false;
				}

				//判断数据类型
				if (propertyinfo.PropertyType == typeof(int) || propertyinfo.PropertyType == typeof(Int64))
				{
					cdtData.IsOnlyNum = true;
				}
				else if (propertyinfo.PropertyType == typeof(double) || propertyinfo.PropertyType == typeof(float) || propertyinfo.PropertyType == typeof(decimal))
				{
					cdtData.IsFloat = true;
				}

				cdtData.ValidateProp = item.ClientPropName.Length > 0 ? item.ClientPropName : "value";

				if (propertyinfo.PropertyType == typeof(string))
					cdtData.IsValidateOnBlur = false;	//字符串禁止使用客户端焦点移开的校验,沈峥修改
				else
					cdtData.IsValidateOnBlur = item.IsValidateOnBlur;

				vobjects = propertyinfo.GetCustomAttributes(typeof(ValidatorAttribute), true);

				if (vobjects.Length <= 0 && !cdtData.IsFloat && !cdtData.IsOnlyNum)
					continue;

				foreach (ValidatorAttribute info in vobjects)
				{
					var targetType = info.GetType();
					var curValidator = info.CreateValidator(targetType, targetType.ReflectedType);
					if (curValidator is IClientValidatable)//add By Fenglilei,2011/11/7,modified by Fenglilei,2012/2/27
					{
						ClientVdtAttribute cvArt = new ClientVdtAttribute(info, propertyinfo);
						cdtData.CvtList.Add(cvArt);

						if (!string.IsNullOrEmpty(cvArt.ClientValidateMethodName))
						{
							Page.ClientScript.RegisterStartupScript(this.GetType(),
																		cvArt.ClientValidateMethodName,
																		((IClientValidatable)curValidator).
																			GetClientValidateScript(), true);
							cvArt.AdditionalData =
								((IClientValidatable)curValidator).GetClientValidateAdditionalData(propertyinfo);
						}
					}
				}

				//this.ControlUserInput(vdtControl, cdtData, item);//新机制,客户端不需要知道校验数据类型,冯立雷修改

				//var pType = propertyinfo.PropertyType;
				//if (item.Format.Length > 0 && (pType == typeof(System.Decimal) || pType == typeof(System.Int32) ||
				//    pType == typeof(System.Double) || pType == typeof(System.Single)))
				//{
				//    cdtData.FormatString = item.Format;
				//}

				this.validatorAttributes.Add(cdtData);
			}
		}