How to add the filter fields programmatically?

4 posts, 0 answers
  1. Bruce
    Bruce avatar
    12 posts
    Member since:
    Oct 2010

    Posted 06 Nov 2010 Link to this post

    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.

  2. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 10 Nov 2010 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Bruce
    Bruce avatar
    12 posts
    Member since:
    Oct 2010

    Posted 10 Nov 2010 Link to this post

    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
  5. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 11 Nov 2010 Link to this post

    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
Back to Top