This is a migrated thread and some comments may be shown as answers.

NullReferenceException in RadAjaxManager.AjaxSettings.AddAjaxSetting()

6 Answers 525 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Nathan J Pledger
Top achievements
Rank 2
Nathan J Pledger asked on 09 Feb 2009, 04:16 PM
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?

6 Answers, 1 is accepted

Sort by
0
SamJ
Top achievements
Rank 1
answered on 10 Feb 2009, 06:17 AM
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
0
Nathan J Pledger
Top achievements
Rank 2
answered on 10 Feb 2009, 09:17 AM
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:

private void AjaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e) 
        { 
            switch (e.Argument) 
            { 
                case "ImportWindowsUsers_NotifyUsers"
 
//                  .... 
 
                    StringBuilder sb = new StringBuilder(); 
                    sb.Append("\n\n<script type=\"text/javascript\">\n//<![CDATA[\n\n"); 
                    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");                        
                    sb.Append("\n\n//]]>\n\n</script>\n\n"); 
 
                    pnlScriptInjector.Controls.Add(new LiteralControl(sb.ToString())); 
                    break
            } 
        } 

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.


0
Accepted
SamJ
Top achievements
Rank 1
answered on 10 Feb 2009, 12:01 PM
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



0
Nathan J Pledger
Top achievements
Rank 2
answered on 10 Feb 2009, 03:57 PM
That definitely works for me.
You should publish more on that, I haven't seen that method before.
0
Sphengle
Top achievements
Rank 1
answered on 20 Jan 2010, 03:37 PM
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);

0
Jack
Top achievements
Rank 1
answered on 27 Apr 2011, 04:56 PM
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). 
Tags
Ajax
Asked by
Nathan J Pledger
Top achievements
Rank 2
Answers by
SamJ
Top achievements
Rank 1
Nathan J Pledger
Top achievements
Rank 2
Sphengle
Top achievements
Rank 1
Jack
Top achievements
Rank 1
Share this question
or