Add RadFilterTextFieldEditor programmatically

5 posts, 0 answers
  1. dimrud
    dimrud avatar
    10 posts
    Member since:
    Aug 2010

    Posted 09 Sep 2010 Link to this post

    Hello,
    I am trying to add RadFilterTextFieldEditor programmatically. It works fine BUT after any action with the field I am getting an error

    Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.

    ASP code

       <telerik:RadAjaxPanel runat="server" ID="RadAjaxPanel1" LoadingPanelID="LoadingPanel1" EnableAJAX="true" >
                 
                    <telerik:RadFilter  runat="server" ID="RadFilter1" ExpressionPreviewPosition="Bottom" Enabled="false" RegisterWithScriptManager="true" EnableEmbeddedScripts="true"  EnableViewState ="true"  >
                    </telerik:RadFilter>
                </telerik:RadAjaxPanel>

    C# code
       RadFilterTextFieldEditor editor = new RadFilterTextFieldEditor();
                editor.FieldName = "field1";
                editor.DataType = typeof(string);
                editor.DisplayName = "Field 1";
                RadFilter1.FieldEditors.Add(editor);
                RadFilterTextFieldEditor editor1 = new RadFilterTextFieldEditor();
                editor1.FieldName = "field2";
                editor1.DataType = typeof(string);
                editor1.DisplayName = "Field 2";
                RadFilter1.FieldEditors.Add(editor1);

    Please help !!!
  2. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 10 Sep 2010 Link to this post

    Hello dimrud,

    Is the C# code pasted placed in the Page_Load event handler? Try modifying it as below and see if it makes any difference:

    RadFilterTextFieldEditor editor = new RadFilterTextFieldEditor();
    RadFilter1.FieldEditors.Add(editor1);
    editor.FieldName = "field1";
    editor.DataType = typeof(string);
    editor.DisplayName = "Field 1";
    RadFilter1.FieldEditors.Add(editor);
    RadFilterTextFieldEditor editor1 = new RadFilterTextFieldEditor();
    editor1.FieldName = "field2";
    editor1.DataType = typeof(string);
    editor1.DisplayName = "Field 2";


    Sincerely yours,
    Iana
    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. dimrud
    dimrud avatar
    10 posts
    Member since:
    Aug 2010

    Posted 10 Sep 2010 Link to this post

    Dear Lana,
    You changed place of 1line
    And now it works fine. Is it not strange behavior of the code ?
    Anyway, Thank you very much.

    My code :

    RadFilterTextFieldEditor editor = new RadFilterTextFieldEditor();
                editor.FieldName = "field1";
                editor.DataType = typeof(string);
                editor.DisplayName = "Field 1";
                RadFilter1.FieldEditors.Add(editor);
    To :

    RadFilterTextFieldEditor editor = new RadFilterTextFieldEditor();
    RadFilter1.FieldEditors.Add(editor1);
    editor.FieldName = "field1";
    editor.DataType = typeof(string);
    editor.DisplayName = "Field 1";

  5. Bruce
    Bruce avatar
    12 posts
    Member since:
    Oct 2010

    Posted 06 Nov 2010 Link to this post

    It does not make sense to me. The new code should give a compiling error because editor1 is not defined yet when it is added to the editor collection.

    Can someone explain this?

    Thanks
  6. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 09 Nov 2010 Link to this post

    Hello Bruce,

    Please excuse me for the misleading code. Here is the proper one:

    RadFilterTextFieldEditor editor = new RadFilterTextFieldEditor(); 
    RadFilter1.FieldEditors.Add(editor); 
    editor.FieldName = "field1"
    editor.DataType = typeof(string); 
    editor.DisplayName = "Field 1"
      
    RadFilterTextFieldEditor editor1 = new RadFilterTextFieldEditor(); 
    RadFilter1.FieldEditors.Add(editor1); 
    editor1.FieldName = "field2"
    editor1.DataType = typeof(string); 
    editor1.DisplayName = "Field 2";

    Note that this code is to be used when adding the editors on Page_Load.
    In case you are adding them on Page_Init, here is the code one should use:

    RadFilterTextFieldEditor editor = new RadFilterTextFieldEditor();   
    editor.FieldName = "field1";  
    editor.DataType = typeof(string);  
    editor.DisplayName = "Field 1";
    RadFilter1.FieldEditors.Add(editor);
        
    RadFilterTextFieldEditor editor1 = new RadFilterTextFieldEditor();  
    editor1.FieldName = "field2";  
    editor1.DataType = typeof(string);  
    editor1.DisplayName = "Field 2";
    RadFilter1.FieldEditors.Add(editor1);

    Note that on Page_Init, the editor properties are first set, and then it is added to the FieldEditors collection. And it is opposite when editors are created on Page_Load: the editor is first added to the FieldEditors collection and then its properties are set. Also on Page_Load editors are added only on initial load (in if(!IsPostBack) statement) whereas on Page_Init are added on each postback.

    I hope this makes it all clear.

    All the best,
    Iana
    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
UI for ASP.NET Ajax is Ready for VS 2017