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

AjaxManager causes error when using System.Web.Caching.Cache property

6 Answers 121 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Mallory
Top achievements
Rank 1
Mallory asked on 09 Mar 2010, 09:46 AM
Hi everyone
I'm fighting with this issue for quite a long time now, and so I thought this would be the right place to ask a question regarding this little issue.

Every time I try to cache data with the System.Web.Caching.Cache property, I get the 'Controls cannot be ajaxified after Page PreRender.'-error at the line where I call AddAjaxSettings on the RadAjaxManager. When I remove the caching lines, everything just works fine. I sincerely do not know why the usage of the Cache property causes the AjaxManager to throw an exception. 

The following describes the faulty scenario:
I have a RadTreeView in the Master page. When a node of it is dropped, some data I want to cache is loaded into a RadGrid which is placed in the Default.aspx page. After this step the thing crashes (the first time - the data is not in the cache yet - it loads the data into the grid; the second time - data is in the cache now - it throws the abovementioned exception). The caching code is actually very simple:
BindingList<string> strings = Cache["LotsOfItems"as BindingList<string>; 
if (strings == null
    strings = MyClass.GetLotsOfStrings(); 
    Cache.Insert("LotsOfItems", strings, null, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration); 
 
myGrid.DataSource = strings; 
myGrid.DataBind(); 


I hope somebody can help me out with this one. I'll provide further information if needed.

Thanks very much.

Kind regards,
Mallory

6 Answers, 1 is accepted

Sort by
0
Yeroon
Top achievements
Rank 2
answered on 09 Mar 2010, 05:17 PM
Hi,

In exactly what event do you use the posted code? Node drop of treeview or page load or prerender override?

/Yeroon
0
Mallory
Top achievements
Rank 1
answered on 10 Mar 2010, 07:25 AM
Hi Yveroon

Thanks for your reply. I use the listed code in the NodeDrop event handler of the RadTreeView

Greetings
Mallory
0
Yeroon
Top achievements
Rank 2
answered on 10 Mar 2010, 09:35 AM
Hi,

Sorry I wasn't very clear in my question. Where do you ajaxify the controls?

So where do you put what you mention here: "error at the line where I call AddAjaxSettings on the RadAjaxManager"

Because it causes a prerender error you might use the wrong moment to add the settings. Also check this thread. It might help:

http://www.telerik.com/community/forums/aspnet-ajax/ajax/radajaxmanagerproxy-updating-controls-inside-a-formview-gives-errors.aspx


/Yeroon
0
Mallory
Top achievements
Rank 1
answered on 10 Mar 2010, 12:16 PM
Hi Yveroon

I actually placed that line of code into the Default.aspx page: ((MasterPage)Master).myAjaxManager.AjaxSettings.AddAjaxSetting(((MasterPage)Master)myTree, myGrid) ). This is done in the PreRender method (actually, if I place it in the Load method, it doesn't work either). 

However, I actually solved the problem, but I'm not sure what exactly caused it to work. I now use a RadAjaxManagerProxy directly on the page (instead of casting the Master property) and everything works fine. It's somehow random, I can't reproduce the error at the moment (Heisenbug?). I think I just handled the usage of the Cache in a wrong way. I accessed the Cache from the wrong places. Now I placed the Cache operations into event handlers and methods that return the actual data (yes, I was doing it wrong, somehow).

Thanks for that link. I had a look at it when the problem arised, but it didn't help. 

I think the problem is solved now. If the problem should appear again, I'll have a deeper look at it. For now everything just works, and that's fine for me :)

Thanks for your time and help.

Regards,
Mallory
0
Yeroon
Top achievements
Rank 2
answered on 10 Mar 2010, 12:32 PM
Hi,

Glad you solved it. The error was caused by the sometimes copmplex life cycle of masterpages, contectpages, usertcontrols etc. It's always a good idea to use a proxymanager if your master already has a main ajaxmanager.

/Yeroon
0
Mallory
Top achievements
Rank 1
answered on 10 Mar 2010, 01:49 PM
Thanks. Yep, the whole lifecycle story is a pretty complex one. Also, I think it's a good practice to place as little code as possible into the Master page. Sometime you have to learn this the hard way (like I did ;)

Regards,
Mallory


Tags
Ajax
Asked by
Mallory
Top achievements
Rank 1
Answers by
Yeroon
Top achievements
Rank 2
Mallory
Top achievements
Rank 1
Share this question
or