Filter.FireApplyCommand and grid created on Page_Init

15 posts, 0 answers
  1. Dmitry
    Dmitry avatar
    121 posts
    Member since:
    Jul 2010

    Posted 08 Dec 2011 Link to this post

    Hello,
    Below is how I initialize grid and filter

    1. Page containing grid:
    protected override void Page_Init(object sender, EventArgs e)
            {
                base.Page_Init(sender, e);
                Grid = new RadGrid(); // actually this is not RadGrid but descendant class I created
                Grid.Initialize(ZSheet, ForceLoad, DataSourceProvider, this); // create columns runtime
                ZSheetContainer.Controls.Add(Grid); 
                GridFilter.Initialize(ZSheet, Grid.ZTable.TableNameUser); 
                if (IsPostBack)
                    GridFilter.ApplyFilter(); // create and apply filter expressions obtained from different source, see below
            }

    2. Create filter expressions and editors:

    public void ApplyFilter()
            {
                if (string.IsNullOrWhiteSpace(_hf.Value)) return;
                List<GridFilterItem> filterItems =
                    new JavaScriptSerializer().Deserialize<List<GridFilterItem>>(_hf.Value);
                foreach (GridFilterItem item in filterItems)
                {
                    ZSheetItem zitem = _table.Structure.FirstOrDefault(t => t.f_name == item.Condition.ColumnID);
                    Type columnType = GetColumnType(zitem);
                    Type filterExpressionType;
                    Type[] types = new[] { columnType };
     
                    switch (item.Condition.Operator)
                    {
                        case GridKnownFunction.Contains:
                            filterExpressionType = typeof(RadFilterContainsFilterExpression);
                            break;
                        case GridKnownFunction.DoesNotContain:
                            filterExpressionType = typeof(RadFilterDoesNotContainFilterExpression);
                            break;
                        case GridKnownFunction.StartsWith:
                            filterExpressionType = typeof(RadFilterStartsWithFilterExpression);
                            break;
                        case GridKnownFunction.EndsWith:
                            filterExpressionType = typeof(RadFilterEndsWithFilterExpression);
                            break;
                        case GridKnownFunction.EqualTo:
                            filterExpressionType = typeof(RadFilterEqualToFilterExpression<>);
                            break;
                        case GridKnownFunction.NotEqualTo:
                            filterExpressionType = typeof(RadFilterNotEqualToFilterExpression<>);
                            break;
                        case GridKnownFunction.GreaterThan:
                            filterExpressionType = typeof(RadFilterGreaterThanFilterExpression<>);
                            break;
                        case GridKnownFunction.LessThan:
                            filterExpressionType = typeof(RadFilterLessThanFilterExpression<>);
                            break;
                        case GridKnownFunction.GreaterThanOrEqualTo:
                            filterExpressionType = typeof(RadFilterGreaterThanOrEqualToFilterExpression<>);
                            break;
                        case GridKnownFunction.LessThanOrEqualTo:
                            filterExpressionType = typeof(RadFilterLessThanOrEqualToFilterExpression<>);
                            break;
                        case GridKnownFunction.Between:
                            filterExpressionType = typeof(RadFilterBetweenFilterExpression<>);
                            types = new[] { columnType, columnType };
                            break;
                        case GridKnownFunction.NotBetween:
                            filterExpressionType = typeof(RadFilterNotBetweenFilterExpression<>);
                            types = new[] { columnType, columnType };
                            break;
                        case GridKnownFunction.IsEmpty:
                            filterExpressionType = typeof(RadFilterIsEmptyFilterExpression);
                            break;
                        case GridKnownFunction.NotIsEmpty:
                            filterExpressionType = typeof(RadFilterNotIsEmptyFilterExpression);
                            break;
                        case GridKnownFunction.IsNull:
                            filterExpressionType = typeof(RadFilterIsNullFilterExpression);
                            break;
                        case GridKnownFunction.NotIsNull:
                            filterExpressionType = typeof(RadFilterNotIsNullFilterExpression);
                            break;
                        default:
                            filterExpressionType = typeof(RadFilterEqualToFilterExpression<>);
                            break;
                    }
     
                    Type genericType = filterExpressionType.MakeGenericType(types);
                    RadFilterExpression expression = (RadFilterExpression)Activator.CreateInstance(genericType, item.Condition.ColumnID);
                    _filter.RootGroup.AddExpression(expression);
                 }            
                _filter.FireApplyCommand();
    
    }
     Now about the problem. When I call ApplyFilter from Page_Load() method, it raises a NullReferenceException in RadFilterDataEditor.CreateEditorFrom() method. When I call it from Page_Init() right after grid is initialized, filter won't apply. What am I doing wrong?
    Thank you.
  2. Marin
    Admin
    Marin avatar
    1045 posts

    Posted 12 Dec 2011 Link to this post

    Hi Dmitry,

     When you construct a filter expression manually you should pass the field name and the value that you filter by. Also make sure that the FieldEditors collection of RadFilter is not empty at this time.
    Here are a few help topics that can be useful in your scenario:
    http://www.telerik.com/help/aspnet-ajax/filter-working-with-expressions.html
    http://www.telerik.com/help/aspnet-ajax/filter-adding-editors.html

    Regards,
    Marin
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Dmitry
    Dmitry avatar
    121 posts
    Member since:
    Jul 2010

    Posted 12 Dec 2011 Link to this post

    I have all required data - field name, function and compare value. Also I have all FieldEditors initialized and set up properly. I have read manuals from your website and set up everything just as said. Result - when I call it from Page_Load, I get a NullReferenceException, when I call it from Page_Init, I get neither exceptions nor filtering.
  5. Marin
    Admin
    Marin avatar
    1045 posts

    Posted 12 Dec 2011 Link to this post

    Hello Dmitry,

     Is the grid already databound at the time you try to apply the filter expressions? This is required because otherwise the filter query cannot be constructed properly. Depending on how you bind the grid it may not have requested the data yet on Page_Load.

    Greetings,
    Marin
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
  6. Dmitry
    Dmitry avatar
    121 posts
    Member since:
    Jul 2010

    Posted 12 Dec 2011 Link to this post

    I bind datasource to grid in NeedDataSource handler so looks like it's not binded by the moment when filter is applied. I'll try this and report results.
    But anyway, why I don't get NullReferenceException in Page_Init but have it raised in Page_Load?
  7. Marin
    Admin
    Marin avatar
    1045 posts

    Posted 13 Dec 2011 Link to this post

    Hi Dmitry,

     The NeedDataSouce normally fires during the PreRender event so in this case the grid will still not be bound on Page_Load. One option here might be to call the FireApplyCommand method later when the grid has bound to the data. The Page_Init event is too early in the life cycle and some of the controls may not have completely initialized yet so the code that throws an exception may not be even executed at this stage. 

    Greetings,
    Marin
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
  8. Dmitry
    Dmitry avatar
    121 posts
    Member since:
    Jul 2010

    Posted 15 Dec 2011 Link to this post

    Hello,
    I tried to apply filter from grid's DataBound handler but anyway I get the same error as in my first post
  9. Marin
    Admin
    Marin avatar
    1045 posts

    Posted 16 Dec 2011 Link to this post

    Hello Dmitry,

     Have you tried constructed and calling the filter expressions in the Page_PreRender event? Do you get the same exception there?

    Greetings,
    Marin
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
  10. Dmitry
    Dmitry avatar
    121 posts
    Member since:
    Jul 2010

    Posted 07 Feb 2012 Link to this post

    Hello,
    I had to suspend custom filtering development for few weeks but now we came to the point when we need it. So it's time to recall everything. Hopefully that was not me on Mars.
    I tried to call FireApplyCommand from Page_PreRender handler and got the same result. Here is stack trace:
    Telerik.Web.UI.RadFilterDataFieldEditor.CreateEditorFrom(RadFilterDataFieldEditor baseEditor) +17
       Telerik.Web.UI.RadFilterSingleExpressionItem.SetupFunctionInterface(Control container) +68
       Telerik.Web.UI.RadFilter.CreateFilterItems() +410
       Telerik.Web.UI.RadFilter.CreateControlHierarchy() +141
       System.Web.UI.Control.EnsureChildControls() +182
       Telerik.Web.UI.RadFilter.HandleApplyCommand() +38
       UpeWeb.ASPNET.UI.Controls.ZSheetGridFilter.ApplyFilter() in D:\Work\UpeWeb.ASPNET2\trunk\UpeWeb.ASPNET.UI\Controls\ZSheetGridFilter.cs:254
       UpeWeb.ASPNET.Views.ZSheetGridView.Page_PreRender(Object sender, EventArgs e) in D:\Work\UpeWeb.ASPNET2\trunk\UpeWeb.ASPNET\Views\ZSheetGridView.ascx.cs:67
    

    Any comments/ideas about that? 
    Thank you.
  11. Marin
    Admin
    Marin avatar
    1045 posts

    Posted 10 Feb 2012 Link to this post

    Hello,

     Before calling the FireApplyCommand method make sure that the _filter.FieldEditors collection is not empty. Also in this collection you should have a FieldEditor with FieldName equal to the one that you have specified in the filter expression: item.Condition.ColumnID. The stack trace indicates that there is no editor for such field.

    Regards,
    Marin
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  12. Rick
    Rick avatar
    5 posts
    Member since:
    May 2012

    Posted 16 May 2012 Link to this post

    Hi, I have a similar issue.
    Actually I have duplicated the example almost exactly from the below URL
    http://demos.telerik.com/aspnet-ajax/filter/examples/customeditors/defaultvb.aspx
    and I get the below error in stack trace
    [NullReferenceException: Object reference not set to an instance of an object.]
       System.Object.GetType() +0
       Telerik.Web.UI.RadFilterDataFieldEditor.CreateEditorFrom(RadFilterDataFieldEditor baseEditor) +17
       Telerik.Web.UI.RadFilterSingleExpressionItem.SetupFunctionInterface(Control container) +68
       Telerik.Web.UI.RadFilter.CreateFilterItems() +410
       Telerik.Web.UI.RadFilter.CreateControlHierarchy() +141
       System.Web.UI.Control.EnsureChildControls() +182
       System.Web.UI.Control.PreRenderRecursiveInternal() +60
       System.Web.UI.Control.PreRenderRecursiveInternal() +222
       System.Web.UI.Control.PreRenderRecursiveInternal() +222
       System.Web.UI.Control.PreRenderRecursiveInternal() +222
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4201

    Additional information:  When I duplicate the above example in a web form without a master page then it works.  Unfortunately, my application is using a master page so it needs to work inside a master page.  I have pinpointed that I get the above error when I don't set the FilterContainerID.  If I set this parameter then I don't get the error but my custom field editors only show as simple text fields instead of dropdown or masked.  My ScriptManager is in the master page because it is needed for other pages.

    Any ideas?
  13. Marin
    Admin
    Marin avatar
    1045 posts

    Posted 17 May 2012 Link to this post

    Hi,

     What kind of control are you trying to filter with RadFilter? In the demo there is an important part of code in the code behind file where we handle the ApplyExpressions event of RadFilter and construct the filter expressions of the grid, the custom editors are also created there in the PageLoad event. Do you have this code in your application as well? The demo should not depend on the presence of master page, maybe you have some other code that is executed there and causes problems.

    Regards,
    Marin
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  14. Rick
    Rick avatar
    5 posts
    Member since:
    May 2012

    Posted 17 May 2012 Link to this post

    Yes I have the Applyexpressions handler. As I said I have duplicated the example in a simple web form without master page which works perfect. Then I have exactly the same code in a web form with master page it doesn't work. In the example it is a radgrid, as I saidi have duplicated exactly the example. The filtered control is a radgrid in my code which is not working The only differences are the data source comes from SQL and the master page
  15. Marin
    Admin
    Marin avatar
    1045 posts

    Posted 22 May 2012 Link to this post

    Hi,

     You can find attached the duplicated example working with a master page.

    I hope this helps.

    Greetings,
    Marin
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  16. Rick
    Rick avatar
    5 posts
    Member since:
    May 2012

    Posted 25 May 2012 Link to this post

    Thanks for the sample with master page.
    Actually I ended up getting it to work on my own by copying the code which was working without masterpage over the same code that wasn't working with masterpage.  Then it worked.  I assume I must have had a small mistake somewhere but I couldn't say where.
    Anyway thanks again.
    Rick
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017