Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error rendering control cache is not available

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.

like image 393
Keltex Avatar asked May 29 '26 19:05

Keltex


1 Answers

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]";
}
like image 196
ssmith Avatar answered Jun 02 '26 07:06

ssmith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!