views:

558

answers:

2

I have a custom control that shows a value obtained from the database (the price of the product). This value is stored in the cache for performance reasons and it works fine. However, in design mode in Visual Studio 2008, I get an error that says, "Error Rendering Control. An unhandled exception has occurred. Cache is not available"

I'm using a ControlDesigner, with a very simple GetDesignTimeHtml:

public override string GetDesignTimeHtml()
{
  return "[$9.99]";
}

I hoped this would fix the problem, but it doesn't.

A: 

Are you directly referencing the Cache object in your ASPX file?

Nick Berardi
+2  A: 

You need to add a wrapper to your Cache access for custom controls, otherwise they will fail in design mode when HttpContext.Current is null. You want to do something like this:

public object GetFromCache(string key)
{
     var myContext = HttpContext.Current;
     if(myContext != null)
     {
         return myContext.Cache[key];
     }
     return "[Design Time Value]";
}
ssmith