NullReferenceException in AddAjaxSetting

10 posts, 0 answers
  1. Dan Ehrmann
    Dan Ehrmann avatar
    165 posts
    Member since:
    Jul 2004

    Posted 25 Jan 2010 Link to this post

    I have finally converted a page from the old Telerik.Web.Control ajax stuff to the new Telerik.Web.UI version. When I call AddAjaxSetting, I get a NullReferenceException. Here is my code:

    ajax_manager.AjaxSettings.AddAjaxSetting(link_type_list, category_list, loading_panel);

    In the debugger, I can see that ajax_manager.GetCurrent() returns null, which I believe is causing the error (the ajax manager variable itself, and my controls are all non-null). This code runs in the page_load event of the ascx control which contains the ajax manager and my controls.

    I am using the latest release - 2009 Q3 SP2.
  2. Pavlina
    Admin
    Pavlina avatar
    6187 posts

    Posted 26 Jan 2010 Link to this post

    Hello Dan,

    Based on the provided information it is hard to determine what is causing the error you received. Could you please send us a sample where we could replicate the issue in order to debug it locally and get to the source of the issue?

    Additionally, you could review the following forum threads which elaborate on similar subject:
    http://www.telerik.com/community/forums/aspnet-ajax/grid/help-with-javascript-rebind.aspx
    http://www.telerik.com/community/forums/aspnet-ajax/ajax/ajaxifying.aspx

    Sincerely yours,
    Pavlina
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Dan Ehrmann
    Dan Ehrmann avatar
    165 posts
    Member since:
    Jul 2004

    Posted 26 Jan 2010 Link to this post

    I do not have a sample that I can send you. Here is a little more info on what I see.

    I get a NullReferenceException. The top entry in the stack trace is:
    at Telerik.Web.UI.AjaxSettingsCollection.AddAjaxSetting(Control ajaxifiedControl, Control updatedControl, RadAjaxLoadingPanel loadingPanel)

    I used Reflector to compare the AddAjaxSetting methods in the old and new RadAjax. The only real difference is that the new one calls RadAjaxManager.GetCurrent(page) so it can call AddedSetting(). This is where the problem seems to be. Calling GetCurrent() in my page returns null, and so I assume it does the same in AddAjaxSetting (as the error message suggests).

    The ajax manager is contained in an ascx which is dynamically loaded in the containing page's OnLoad handler. In the debugger, I can see that the control is loaded and added to the page before AddAjaxSetting gets called. I used this same scheme for all my pages, many of which use other Telerik.Web.UI controls, and there are no problems. The script manager control is in its proper place.

    My guess is that the ajax manager is not getting added to the Page.Items collection. I have no idea where that is supposed to happen, but I hope it is a clue for you.
  4. Pavlina
    Admin
    Pavlina avatar
    6187 posts

    Posted 28 Jan 2010 Link to this post

    Hi Dan,

    Does using a bit different syntax, namely:
    C#
    radMgr = $find("<%= RadAjaxManager.GetCurrent(this).ClientID %>");

    makes a difference?

    Sincerely yours,
    Pavlina
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Dan Ehrmann
    Dan Ehrmann avatar
    165 posts
    Member since:
    Jul 2004

    Posted 28 Jan 2010 Link to this post

    This problem occurs in my codebehind file, not in the ascx file, so the code you suggest will not work. I am not directly calling GetCurrent() in my code - I am calling AddAjaxSetting() which internally calls GetCurrent(). If I call GetCurrent() myself as a test while debugging, it returns null.

    Here is the code I use to call AddAjaxSetting:

    ajax_manager.AjaxSettings.AddAjaxSetting(link_type_list, category_list, loading_panel);

    When I call this, the ajax_manager variable is *not* null - it is correctly pointing to the ajax manager control on the page. Same for the controls passed as parameters - none of them are null.

  6. Pavlina
    Admin
    Pavlina avatar
    6187 posts

    Posted 01 Feb 2010 Link to this post

    Hi Dan,

    I am afraid I could hardly guest what is the cause of the error based on the provided information. Could you please elaborate a bit more on your scenario. It will be best if you could send us the problematic page code. Thus I could do all my best to provide a solution.

    However, I suggest you add the code on Page_PreRender event instead of Page_Load and see if it makes a difference.

    Greetings,
    Pavlina
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  7. Dan Ehrmann
    Dan Ehrmann avatar
    165 posts
    Member since:
    Jul 2004

    Posted 01 Feb 2010 Link to this post

    I did try prerender, and got the same result. I moved the ajax settings stuff to the ascx file, and it worked fine.

    So, my problem is resolved for this page - it turns out I didn't really need to do the settings programmatically. However, it may be something you can look into. My ascx is not complex - I just added a couple of simple ajax settings to a simple ajax manager. The issue may be in the way the ascx is loaded, as I described in a previous post. It is part of a large framework, and I really can't come up with a sample project for you to run.
  8. Pavlina
    Admin
    Pavlina avatar
    6187 posts

    Posted 04 Feb 2010 Link to this post

    Hello Dan ,

    Thank you for providing additional information. Note that when the RadAjaxManager control is used in dynamically loaded user control, you should create RadAjaxManager on Page_Init event and after that add AjaxSettings on Page_Load or Page_PreRender.

    Sincerely yours,
    Pavlina
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  9. Dan Ehrmann
    Dan Ehrmann avatar
    165 posts
    Member since:
    Jul 2004

    Posted 04 Feb 2010 Link to this post

    My ajax manager is declared in the ascx file, rather than being created in the Init event. Would I need to create the ajax manager in my code instead of declaratively to allow this to work?
  10. Pavlina
    Admin
    Pavlina avatar
    6187 posts

    Posted 05 Feb 2010 Link to this post

    Hi Dan,

    Indeed you should create RadAjaxManager on Init instead of creating it declaratively in ascx file. Thus your project should work properly.

    Sincerely yours,
    Pavlina
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
Back to Top