This is a migrated thread and some comments may be shown as answers.

RadFilter - Expression Value Always Empty in DropDownList

1 Answer 103 Views
Filter
This is a migrated thread and some comments may be shown as answers.
JD
Top achievements
Rank 1
JD asked on 16 Sep 2013, 07:23 PM
Hi,

I have a very simple dynamically-created filter.  The expression value is always empty for a standard DropDownList expression, but works fine with the TextBox; I'm sure it's my own ignorance but I can't find syntax to get the input value and/or set the expression value, which is apparently done in the OnExpressionEvaluated event.  The textbox works without any intervention.

I'm new to Telerik so apologies if there is some blatant issue with the code.  There is a comment where I believe I'm going wrong.

Thanks,
JD

public class filterSimpleCtrl : WebControl
    {
        private RadFilter _rf = new RadFilter();
        private Label _label = new Label();
        private RadScriptManager sm = new RadScriptManager();
 
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Page.Form.Controls.Add(sm);
 
            this._label.ID = "_label";
 
            this._rf.ID = "_rf";
            this._rf.FilterContainerID = "RadGrid1";
            this._rf.ExpressionPreviewPosition = RadFilterExpressionPreviewPosition.Bottom;
            this._rf.ApplyExpressions += _rf_ApplyExpressions;
            this._rf.ExpressionItemCreated += _rf_ExpressionItemCreated;
 
            RadFilterTextFieldEditor rfe2 = new RadFilterTextFieldEditor();
            rfe2.FieldName = "City";
            rfe2.DisplayName = "City";
            rfe2.DefaultFilterFunction = RadFilterFunction.Contains;
            _rf.FieldEditors.Add(rfe2);
             
            RadFilterDropDownEditor rfe = new RadFilterDropDownEditor();
            rfe.FieldName = "CountryCode";
            rfe.DisplayName = "Country Code";
            rfe.DataTextField = "CountryCode";
            rfe.DefaultFilterFunction = RadFilterFunction.EqualTo;
            _rf.FieldEditors.Add(rfe);
 
 
            this.Page.Form.Controls.Add(_rf);
            this.Page.Form.Controls.Add(new LiteralControl("<br />"));
            this.Page.Form.Controls.Add(_label);
        }
 
        protected void _rf_ExpressionItemCreated(object sender, RadFilterExpressionItemCreatedEventArgs e)
        {
 
            // Populate the RadDropDownList control for every ShipCountry field editor
            RadFilterSingleExpressionItem singleItem = e.Item as RadFilterSingleExpressionItem;
            if (singleItem != null && singleItem.FieldName == "CountryCode" && singleItem.IsSingleValue)
            {
                RadDropDownList dropDownList = singleItem.InputControl as RadDropDownList;
                dropDownList.DataSource = GetListData("country");
                dropDownList.DataBind();
            }
 
            // Removes the AddGroupExpressionButton for group expression other than the root group
            RadFilterGroupExpressionItem groupItem = e.Item as RadFilterGroupExpressionItem;
            if (groupItem != null)
            {
                if (groupItem.IsRootGroup) groupItem.RemoveButton.Visible = false;
                else groupItem.AddGroupExpressionButton.Visible = false;
            }
        }
 
        void _rf_ApplyExpressions(object sender, RadFilterApplyExpressionsEventArgs e)
        {
            RadFilterSqlQueryProvider queryProvider = new RadFilterSqlQueryProvider();
            queryProvider.OnExpressionEvaluated = this.ExpressionEvaluated;
            queryProvider.ProcessGroup(e.ExpressionRoot);
            this._label.Text = queryProvider.Result;
        }
 
        private void ExpressionEvaluated(RadFilterEvaluationData evaluationData)
        {
            RadFilterFunction filterFunction = evaluationData.Expression.FilterFunction;
            if (evaluationData.Expression.FieldName == "CountryCode" &&
                filterFunction == RadFilterFunction.EqualTo ||
                filterFunction == RadFilterFunction.NotEqualTo)
            {
                //NONE OF THESE WORK
                //string value = (evaluationData.Expression as RadFilterContainsFilterExpression).Value;
                //string value = (((IRadFilterValueExpression)evaluationData.Expression).Values[0] as RadFilterDropDownEditor).ExtractValues()[0].ToString();
                //string value = (((IRadFilterValueExpression)evaluationData.Expression).Values[0] as RadDropDownList).SelectedValue;
 
                RadFilterNonGroupExpression expression = null;
                RadFilterSqlExpressionEvaluator evaluator = RadFilterSqlExpressionEvaluator.GetEvaluator(evaluationData.Expression.FilterFunction);
                expression = new RadFilterEqualToFilterExpression<string>("CountryCode") { Value = value };
                evaluator.GetEvaluationData(expression).CopyTo(evaluationData);
            }
        }
 
        private DataTable GetListData(string list)
        {
            string query = "";
            switch (list)
            {
                case "country":
                    query = "SELECT CountryCode FROM Country";
                    break;
                default:
                    break;
            }
            string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            SqlCommand cmd = new SqlCommand(query);
            using (SqlConnection con = new SqlConnection(conString))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
 
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }

1 Answer, 1 is accepted

Sort by
0
JD
Top achievements
Rank 1
answered on 17 Sep 2013, 04:07 PM
Nevermind, it appears that the DataValue field is required in order for the data to be saved.  Newbie error... :(
Tags
Filter
Asked by
JD
Top achievements
Rank 1
Answers by
JD
Top achievements
Rank 1
Share this question
or