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

How to add the filter fields programmatically?

3 Answers 100 Views
Filter
This is a migrated thread and some comments may be shown as answers.
Bruce
Top achievements
Rank 1
Bruce asked on 06 Nov 2010, 08:56 AM
Hi,

If I want the filter field dropdown to be populated with "PersonId", "PersonName", "PersonCity", should I create three RadFilterTextFieldEditor objects and add them to the FieldEditors collection?

I stored the field names in a database. I get the data from the database and create the editors in the Page_Load.

if (!IsPostBack)
{
      List<FilterField> fields = db.GetData();
      foreach (FilterField field in fields)
      {
            RadFilterTextFieldEditor editor = new RadFilterTextFieldEditor();
            editor.FieldName = field.Name;
            RadFilter1.FieldEditors.Add(editor);
      }
}

It works when the page is first loaded, however, I got an error in any following postback.

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

I appreciate it very much if you can help.

3 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 10 Nov 2010, 10:05 AM
Hi Bruce,

Try adding the filter fields editors on the OnInit event without the !IsPostBack condition.

Hope it helps.

Regards,
Tsvetoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Bruce
Top achievements
Rank 1
answered on 10 Nov 2010, 06:55 PM
Hi Tsvetoslav,

It was fixed by adding the editor to the collection first then setting the Fieldname:

            RadFilterTextFieldEditor editor = new RadFilterTextFieldEditor();
            RadFilter1.FieldEditors.Add(editor);
            editor.FieldName = field.Name;

I don't know why but it works.

Thanks for the help.
Bruce
0
Tsvetoslav
Telerik team
answered on 11 Nov 2010, 09:17 AM
Hello Bruce,

When creating the RadFilter control (or any other Rad control for that matter) in the Page_Load event you should first add its sub controls to its Controls Collection (as in the case of the field editors) and afterwards set their properties so that they properly track their ViewState. An alternative approach is to use the Page_Init event, as pointed out in my previous post, when ViewState for tha page and the controls on it has not yet been loaded. 

Best wishes,
Tsvetoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Filter
Asked by
Bruce
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Bruce
Top achievements
Rank 1
Share this question
or