0

来自http://msdn.microsoft.com/en-us/library/f5db6z8k(v=vs.100).aspx

我创建了一个 customeValidators.cs 页面,用于检查输入的数据是否存在于 Db 中。这行得通。但是,当我尝试从必要的页面中调用它时

protected void runUniqueReference(object source, ServerValidateEventArgs args)
{
    args.IsValid = (CustomValidators.UniqueReference(BOQRefTextBox.Text));
}

我收到错误消息CustomValidators.UniqueReference,无法将“数据注释”转换为“布尔”任何想法?编辑;

 public static ValidationResult UniqueReference(string Reference)
    {
        ContextDB db = new  ContextDB();

        var lisStoredInDB =
            (from Bill in db.Bill_Of_Quantities
             where Bill.Reference == Reference
             select Bill.Reference).ToList();

        if (lisStoredInDB.Count != 0)
        {
            return new ValidationResult(string.Format("This reference is already stored in the database, Please enter another"));
        }

        return ValidationResult.Success;
    }
4

1 回答 1

1

args.IsValid是 type bool,并且CustomValidators.UniqueReference不能返回该类型的值。所以,

args.IsValid = (CustomValidators.UniqueReference(BOQRefTextBox.Text));

将不起作用,因为您无法将返回值分配UniqueReferenceIsValid.

由于UniqueReference返回一个ValidationResult,它应该看起来像这样:

args.IsValid = (CustomValidators.UniqueReference(BOQRefTextBox.Text)).IsValid;
于 2013-01-25T12:51:07.723 回答