Esempio n. 1
0
 /// <summary>
 /// Verify that the current Key Type is set to a specific value, or throw an Exception.
 /// </summary>
 /// <param name="keyType"></param>
 private void VerifyRequiredKeyTypeOrThrow(SingleEntityPickerKeySpecifier keyType)
 {
     if (keyType == SingleEntityPickerKeySpecifier.Guid &&
         _Key != SingleEntityPickerKeySpecifier.Guid)
     {
         throw new Exception("Invalid Operation. This operation is only available when KeyType is set to \"Guid\".");
     }
     else if (keyType == SingleEntityPickerKeySpecifier.Id &&
              _Key != SingleEntityPickerKeySpecifier.Id)
     {
         throw new Exception("Invalid Operation. This operation is only available when KeyType is set to \"Id\".");
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Initializes the list of available items.
        /// </summary>
        /// <param name="entities"></param>
        /// <param name="itemDescriptionExpression"></param>
        /// <param name="keyType"></param>
        /// <param name="allowEmptySelection"></param>
        public void InitializeListItems(IEnumerable <TEntity> entities, Expression <Func <TEntity, string> > itemDescriptionExpression, SingleEntityPickerKeySpecifier keyType = SingleEntityPickerKeySpecifier.Id, bool allowEmptySelection = false)
        {
            var mexpr = itemDescriptionExpression.Body as MemberExpression;

            if (mexpr == null)
            {
                return;
            }

            var func = itemDescriptionExpression.Compile();

            this.Items.Clear();

            _Key = keyType;

            if (allowEmptySelection)
            {
                this.Items.Add(new ListItem());
            }

            foreach (var entity in entities)
            {
                var description = func(entity);

                string key;

                if (_Key == SingleEntityPickerKeySpecifier.Guid)
                {
                    key = entity.Guid.ToString();
                }
                else
                {
                    key = entity.Id.ToString();
                }

                this.Items.Add(new ListItem(description, key));
            }
        }