private void DeleteCriteria(DataListCommandEventArgs e)
        {
            var id = ((Label)e.Item.FindControl("lblId")).Text;

            var selectedItem = SelectedSearchFields.FirstOrDefault(o => o.Id == new Guid(id));

            SelectedSearchFields.Remove(selectedItem);
            dlSearchCriteria.DataSource = SelectedSearchFields;
            dlSearchCriteria.DataBind();
        }
        private void AddNewCriteria(DataListCommandEventArgs e)
        {
            DropDownList ddlField = (DropDownList)e.Item.FindControl("ddlFields");
            var          field    = Manager.Fields.First(o => o.DisplayName == ddlField.SelectedValue);

            Control ddlOperator = e.Item.FindControl("ddlOperators");

            string oper            = string.Empty;
            string val             = string.Empty;
            string userFriendlyVal = string.Empty;

            if (ddlOperator != null)
            {
                oper = (ddlOperator as DropDownList).SelectedValue;
            }

            var key = Request.Params.AllKeys.FirstOrDefault(o => o.Contains("CtrlValue"));

            if (!string.IsNullOrWhiteSpace(key))
            {
                val = Request.Params[key];

                if (field.FieldType.Type == AdvancedSearch.Enums.FieldTypeName.KeyValue)
                {
                    userFriendlyVal = (field.FieldType as AdvancedSearch.KeyValueType).KeyValues.First(o => o.Key == val).Value;
                }
                else
                {
                    userFriendlyVal = val;
                }
            }
            else if (field.FieldType.Type != AdvancedSearch.Enums.FieldTypeName.Boolean)
            {
                // show error
            }



            SelectedSearchFields.Add(new SelectedSearchField()
            {
                fieldDisplayName    = field.DisplayName,
                operatorDisplayName = oper,
                value             = val,
                UserFriendlyValue = userFriendlyVal
            });

            dlSearchCriteria.DataSource = SelectedSearchFields;
            dlSearchCriteria.DataBind();
        }
        private void SetValues()
        {
            AdvancedSearch.Manager m = Manager;

            m.ClearSelectedValues();

            AdvancedSearch.Field field;
            foreach (var criteria in SelectedSearchFields.Where(o => o.fieldDisplayName != "")) // Eliminate dummy criteria needed by datalist
            {
                field = m.Fields.FirstOrDefault(o => o.DisplayName == criteria.fieldDisplayName);

                var newValue = new AdvancedSearch.SelectedValue();

                newValue.Operator = field.FieldType.Operators.FirstOrDefault(o => o.DisplayName == criteria.operatorDisplayName);

                if (field.FieldType.Type == AdvancedSearch.Enums.FieldTypeName.KeyValue)
                {
                    newValue.Value = criteria.UserFriendlyValue;
                }
                else
                {
                    newValue.Value = criteria.value;
                }

                field.SelectedValues.Add(newValue);
            }

            if (rblAndOr.SelectedValue == "AND")
            {
                Manager.SetFilterType(AdvancedSearch.Enums.FilterType.AND);
            }
            else
            {
                Manager.SetFilterType(AdvancedSearch.Enums.FilterType.OR);
            }
        }