NullReferenceException in RadAjaxManager.AjaxSettings.AddAjaxSetting()

7 posts, 1 answers
  1. Nathan J Pledger
    Nathan J Pledger avatar
    95 posts
    Member since:
    Apr 2006

    Posted 09 Feb 2009 Link to this post

    Hi,

    I have a Page Controller design pattern, so I have a base Page class that defines and dumps out a RadAjaxManager control. The (edited) base class is:

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Web; 
    using System.Xml; 
    using xxx.yyy.ASPNET.Security; 
    using xxx.yyy.Client; 
    using xxx.yyy.Client.Security; 
    using xxx.yyy.Core; 
    using Telerik.Web.UI; 
     
    namespace xxx.yyy.ASPNET 
        public class AuthenticatedyyyPage : yyyPageBase 
        { 
            private RadAjaxManager _radAjaxManager=null
            #region ~ Properties ~ 
     
            public RadAjaxManager AjaxManager 
            { 
                get { return _radAjaxManager; } 
            } 
            #endregion 
     
            protected override void OnLoad(EventArgs e) 
            { 
                // set up global AJAX Manager 
                _radAjaxManager = BuildRadAjaxManager(); 
                Form.Controls.Add(_radAjaxManager); 
     
                if (!ClientScript.IsClientScriptBlockRegistered(_radAjaxManager.ClientID)) 
                { 
                    ClientScript.RegisterClientScriptBlock(_radAjaxManager.GetType(), _radAjaxManager.ClientID,GenerateJavascript(),true); 
                } 
                 
     
                base.OnLoad(e); 
            } 
     
            protected virtual RadAjaxManager BuildRadAjaxManager() 
            { 
                RadAjaxManager radAjaxManager = new RadAjaxManager(); 
                radAjaxManager.ID = "radAjaxManager";    
                return radAjaxManager; 
            } 
     
            private string GenerateJavascript() 
            { 
                StringBuilder sb = new StringBuilder(); 
     
                 
     
                sb.Append("\n\nfunction migGetRadAjaxManager() {\n"); 
                sb.Append("\tvar ajaxManager=$find(\"" + _radAjaxManager.ClientID + "\");\n"); 
                sb.Append("\treturn ajaxManager;\n"); 
                sb.Append("}\n\n"); 
     
     
                return sb.ToString(); 
            } 
        } 
     
     

    (The RadAjaxManager is added into the Form and appears in the rendered HTML)

    In the derived page, I want to add an AjaxSetting programmatically (as per http://www.telerik.com/help/aspnet-ajax/ajxaddajaxsettingsprogrammatically.html) in the Page_Load():

    protected void Page_Load(object sender, EventArgs e) 
            { 
                AjaxManager.AjaxRequest += new RadAjaxControl.AjaxRequestDelegate(AjaxManager_AjaxRequest); 
                 
                AjaxManager.AjaxSettings.AddAjaxSetting(btnCancel0, pnlScriptInjector); 
                 
            } 

    But this results in the exception:

    Object reference not set to an instance of an object.
    AjaxManager.AjaxSettings.AddAjaxSetting(btnCancel0, pnlScriptInjector); 
                 

       at Telerik.Web.UI.AjaxSettingsCollection.AddAjaxSetting(Control ajaxifiedControl, Control updatedControl) 
       at xxx.yyy.ASPNET.Presentation.Secure.Admin.ImportWindowsUsers.Page_Load(Object sender, EventArgs e) in c:\dev2008\yyy2\yyy_WEB\secure\admin\ImportWindowsUsers.aspx.cs:line 128 
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) 
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) 
       at System.Web.UI.Control.OnLoad(EventArgs e) 
       at xxx.yyy.ASPNET.yyyBase.OnLoad(EventArgs e) in C:\dev2008\yyy2\xxx.yyy.ASPNET\yyyPageBase.cs:line 45 
       at xxx.yyy.ASPNET.Authenticatedyyy.OnLoad(EventArgs e) in C:\dev2008\yyy\xxx.yyy.ASPNET\AuthenticatedyyyPage.cs:line 138 
       at System.Web.UI.Control.LoadRecursive() 
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

    Inspecting the variables in the debugger and I can't see anything that is null.

    Could you please tell me why is this happening? Am I using it right?

  2. SamJ
    SamJ avatar
    101 posts
    Member since:
    Jul 2008

    Posted 10 Feb 2009 Link to this post

    Hi Nathan,

    Have you tried adding the RadAjaxManager to the form controls collection OnInit instead of OnLoad event? I think that when the manager is added later than OnInit it is unable to initialize properly.

    Does this helps?

    Regards,
    SamJ
  3. Nathan J Pledger
    Nathan J Pledger avatar
    95 posts
    Member since:
    Apr 2006

    Posted 10 Feb 2009 Link to this post

    hi,

    Thanks, that works fine, though I have had to put my code in a RadCodeBlock.

    The particular case does not work, however.

    I have the following scenario:

    User clicks a toolbar button (a wrapper control for RadToolbar), behaviour of which is defined by the AjaxSetting:

            protected override void OnPreRender(EventArgs e) 
            { 
                AjaxManager.AjaxSettings.AddAjaxSetting(btnSelectUsers, pnlScriptInjector); 
     
                base.OnPreRender(e); 
            } 
     
     

    Toolbar runs JavaScript that calls the RadAjaxManager.AjaxRequest method:

    ajaxManager.ajaxRequest('ImportWindowsUsers_NotifyUsers'

    This then runs fine, which sends some emails off, and I then inject some Javascript into the pnlScriptInjector panel:

    1 private void AjaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e) 
    2         { 
    3             switch (e.Argument) 
    4             { 
    5                 case "ImportWindowsUsers_NotifyUsers"
    6  
    7 //                  .... 
    8  
    9                     StringBuilder sb = new StringBuilder(); 
    10                     sb.Append("\n\n<script type=\"text/javascript\">\n//<![CDATA[\n\n"); 
    11                     sb.Append("radalert(\"<strong>" + emailsSent.ToString("#,##0") + "</strong> email successfully sent to users.<br /><br />");
    12                     if (emailsNotSent>0) sb.Append("<strong>"+emailsNotSent.ToString("#,##0")+"</strong> users could not be emailed because they do not have Email Addresses available within their Windows User Account record.<br /><br />");
    13                     if (emailsFailed>0) sb.Append("<strong class=\"red\">"+emailsFailed.ToString("#,##0")+"</strong> users may not have been emailed due to errors in sending the email.<br /><br />");
    14                     sb.Append("As a precaution, consider Exporting the created User records to a <acronym title=\"Comma Separated Value\">CSV</acroynym> or Microsoft Excel file.\",400,200,\"Users Notified\");\n");                        
    15                     sb.Append("\n\n//]]>\n\n</script>\n\n"); 
    16  
    17                     pnlScriptInjector.Controls.Add(new LiteralControl(sb.ToString())); 
    18                     break
    19             } 
    20         } 

    Am I being too adventurous/optimistic to expect the AjaxSetting handle my injecting JavaScript into the pnlScriptInjector for it to run following the server-side operation? No script appears in the pnlScriptInjector element when I inspect the DOM post AJAX.


  4. Answer
    SamJ
    SamJ avatar
    101 posts
    Member since:
    Jul 2008

    Posted 10 Feb 2009 Link to this post

    Hi Nathan,

    I haven't try this approach for script injection. But I assume that you could use the ResponseScript property of the ajax manager for this purpose. So your modified code should be something like this:

    private void AjaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e)  
    {  
         switch (e.Argument)  
         {  
              case "ImportWindowsUsers_NotifyUsers":  
                //                  ....  
                    StringBuilder sb = new StringBuilder();  
                    sb.Append("radalert(\"<strong>" + emailsSent.ToString("#,##0") +
                        "</strong> email successfully sent to users.<br /><br />");
                   if (emailsNotSent>0) sb.Append("<strong>" + emailsNotSent.ToString("#,##0") + 
                      "</strong> users could not be emailed because they do not have Email Addresses available within their Windows User Account record.<br /><br />");
                   if (emailsFailed>0) sb.Append("<strong class=\"red\">" + emailsFailed.ToString("#,##0") + 
                      "</strong> users may not have been emailed due to errors in sending the email.<br /><br />");
               sb.Append("As a precaution, consider Exporting the created User records to a <acronym title=\"Comma Separated Value\">CSV</acroynym> or Microsoft Excel file.\",400,200,\"Users Notified\");\n");                         
               ajaxManager.ResponseScripts.Add(sb.ToString()); 
                   break;  
        }  

    Does this works for you?

    Thanks
    SamJ



  5. Nathan J Pledger
    Nathan J Pledger avatar
    95 posts
    Member since:
    Apr 2006

    Posted 10 Feb 2009 Link to this post

    That definitely works for me.
    You should publish more on that, I haven't seen that method before.
  6. Sphengle
    Sphengle avatar
    29 posts
    Member since:
    Sep 2007

    Posted 20 Jan 2010 Link to this post

    I too had the same exception using the AddAjaxSetting() method.  Nothing seemed to work for me except this long winded approach....


    AjaxSetting

     

    setting = new AjaxSetting();

     

     

    AjaxUpdatedControl target = new AjaxUpdatedControl (rgdTLA.ID, rlp.ID );

     

    setting.UpdatedControls.Add(target);

    setting.AjaxControlID = lnk.ID ;

     

    radAjaxManager.AjaxSettings.Add(setting);

  7. Jack
    Jack avatar
    4 posts
    Member since:
    Feb 2011

    Posted 27 Apr 2011 Link to this post

    I had the same issue as well.  This was remedied by ensuring that I had specified a value for the ID property of the ajaxifiedControl parameter (that being the control initiating an update). 
Back to Top