Exemplo n.º 1
0
 /// <summary> Switch: set value. </summary>
 public void Switch(bool value)
 {
     if (this.Items != null && this.Items.Count > 0)
     {
         CheckboxItem cbi = this.Items.FirstOrDefault(x => x.Value == bool.TrueString);
         if (cbi != null)
         {
             cbi.Selected = value;
         }
     }
     this.Value = new string[] { value.ToString() };
 }
Exemplo n.º 2
0
 /// <summary> Switch: get value. </summary>
 public bool Switch()
 {
     if (this.Value != null)
     {
         string v = this.Value.FirstOrDefault();
         return(string.Compare(v, bool.TrueString, true) == 0);
     }
     else
     {
         if (this.Items != null && this.Items.Count > 0)
         {
             CheckboxItem cbi = this.Items.FirstOrDefault(x => x.Value == bool.TrueString);
             if (cbi != null)
             {
                 return(cbi.Selected);
             }
         }
         return(false);
     }
 }
Exemplo n.º 3
0
        /// <summary> Add Item. </summary>
        public CheckboxItem AddItem(int id)
        {
            if (this.Items == null)
            {
                this.Items = new List <CheckboxItem>();
            }

            CheckboxItem sibling = this.Items.LastOrDefault();

            // Default
            CheckboxItem item = new CheckboxItem
            {
                Id       = id,
                Label    = null,
                Value    = null,
                Index    = (sibling != null ? sibling.Index + 1 : 0),
                Selected = false
            };

            this.Items.Add(item);

            return(item);
        }
Exemplo n.º 4
0
        /// <summary> Set Form Value. </summary>
        /// <param name="elementId"> The form element id. </param>
        /// <param name="typeId"> The form element type id. </param>
        /// <param name="value"> The form value. </param>
        public void SetValue(int formLayoutId, object value)
        {
            IEnumerable <FormElement> elements = GetAllElements();

            FormElement element = elements.Where(x => x.Id == formLayoutId).FirstOrDefault();

            if (element == null)
            {
                return;
            }

            if (element.Type == FormElementType.Block)
            {
            }
            else if (element.Type == FormElementType.Html)
            {
                HtmlElement html = element as HtmlElement;

                html.Value = Convert.ToString(value);
            }
            else if (element.Type == FormElementType.Input)
            {
                Input input = element as Input;

                input.Value = Convert.ToString(value);
            }
            else if (element.Type == FormElementType.Textarea)
            {
                Textarea textarea = element as Textarea;

                textarea.Value = Convert.ToString(value);
            }
            else if (element.Type == FormElementType.Dropdown)
            {
                Dropdown dropdown = element as Dropdown;

                // Vast value to string array
                string[] valueArray = null;

                if (value != null)
                {
                    Type valueType         = value.GetType();
                    Type genType           = valueType.IsGenericType ? valueType.GetGenericTypeDefinition() : null;
                    bool valueIsEnumerable = valueType.IsArray || (genType != null && (genType == typeof(List <>) || genType == typeof(IEnumerable <>)));

                    if (valueIsEnumerable)
                    {
                        valueArray = ((IEnumerable)value).Cast <object>().Select(x => x.ToString()).ToArray();
                    }
                    else
                    {
                        string sValue = Convert.ToString(value);
                        if (sValue != null)
                        {
                            valueArray = new string[] { sValue };
                        }
                    }
                }

                // Dropdown with items, or ajax source
                if (dropdown.Items != null && dropdown.Items.Count > 0)
                {
                    // Deselect every item
                    foreach (DropdownItem itemX in dropdown.Items)
                    {
                        itemX.Selected = false;
                    }

                    // Values set?
                    if (valueArray != null)
                    {
                        // Valid item placeholder
                        List <string> validItems = new List <string>();

                        // Loop over every value in the value array
                        foreach (string val in valueArray)
                        {
                            // Select item
                            DropdownItem item = dropdown.Items.Where(x => x.Value != null && x.Value.Equals(val, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                            // Check if selected item exists
                            if (item != null)
                            {
                                item.Selected = true;
                                validItems.Add(item.Value);
                            }
                        }

                        // Set dropdown value
                        dropdown.Value = validItems.ToArray();
                    }
                }
                else
                {
                    // No items, maybe it's an ajax source
                    if (dropdown.AjaxSource != null)
                    {
                        dropdown.Value = valueArray;
                    }
                }
            }
            else if (element.Type == FormElementType.Radiobutton)
            {
                Radiobutton radio = element as Radiobutton;

                string sValue = Convert.ToString(value);

                RadiobuttonItem item = radio.Items.Where(x => x.Value.Equals(sValue, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                if (item != null)
                {
                    foreach (RadiobuttonItem itemX in radio.Items)
                    {
                        itemX.Selected = false;
                    }

                    item.Selected = true;

                    radio.Value = sValue;
                }
            }
            else if (element.Type == FormElementType.Checkbox)
            {
                Checkbox checkbox = element as Checkbox;

                // Vast value to string array
                string[] valueArray = null;
                Type     valueType  = value.GetType();
                if (valueType.IsArray)
                {
                    valueArray = ((IEnumerable)value).Cast <object>().Select(x => x.ToString()).ToArray();
                }
                else
                {
                    string sValue = Convert.ToString(value);
                    valueArray = new string[] { sValue };
                }

                // Deselect every item
                foreach (CheckboxItem itemX in checkbox.Items)
                {
                    itemX.Selected = false;
                }

                // Values set?
                if (valueArray != null)
                {
                    // Valid item placeholder
                    List <string> validItems = new List <string>();

                    // Loop over every value in the value array
                    foreach (string val in valueArray)
                    {
                        CheckboxItem item = checkbox.Items.Where(x => x.Value.Equals(val, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                        if (item != null)
                        {
                            item.Selected = true;
                            validItems.Add(item.Value);
                        }
                    }

                    // Set checkbox value
                    checkbox.Value = validItems.ToArray();
                }
            }
            else if (element.Type == FormElementType.Calendar)
            {
                Calendar calendar = element as Calendar;
                calendar.SetElementValue(value);
            }
            else if (element.Type == FormElementType.Hidden)
            {
                Hidden hidden = element as Hidden;

                hidden.Value = Convert.ToString(value);
            }
            else if (element.Type == FormElementType.Tagging)
            {
                Tagging tagging = element as Tagging;

                tagging.Value = (List <TaggingItem>)value;
            }
            else if (element.Type == FormElementType.Link)
            {
                Link obj = element as Link;

                obj.SetElementValue(value);
            }
            else if (element.Type == FormElementType.Label)
            {
                Label label = element as Label;
                label.SetElementValue(value);
            }
        }