Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 837000 - Last Review: March 12, 2004 - Revision: 1.3
The PartialCachingControl.CachedControl property always returns a null reference
If a user control (.ascx file) contains an @ OutputCache directive, Microsoft ASP.NET automatically generates a PartialCachingControl class when the user control is requested. The PartialCachingControl class contains a CachedControl property (the PartialCachingControl.CachedControl property). You can use the CachedControl property to determine if the control was pulled from the cache or if the control was newly constructed. If the value of this property is a null reference (Nothing in Microsoft Visual Basic), the user control was pulled from the cache. Â If the value of this property is not a null reference, the user control was constructed. However, if the CachedControl is not coded correctly, the CachedControl property will always return a null reference. The CachedControl property always returns a null reference unless the control has been pre-added to the control tree. Before you check the CachedControl property, add the user control to the control tree. Note the code sample that is in the "More Information" section of this article.
Note This behavior is by design. The cached control is not created until the Init phase. The control is not created until you insert the PartialCachingControl class in the control tree.
For more information, see a code sample in the "More Information" section of this article.
The following code demonstrates that you have a user control that contains an
OutPutCache directive, you can use the
PartialCachingControl class to determine if the user control was pulled from the cache or if the user control was newly constructed:
void Page_Init()
{
Control Item;
     Item = Page.LoadControl("test1.ascx");
     Test1a.Controls.Add(Item); // Comment this line to see the "always null" behavior.
     if ( ((PartialCachingControl)Item).CachedControl != null)
{
     Response.Write("CachedControl is not null!");
     }
else
{
     Response.Write("CachedControl is null!");
   }
}
APPLIES TO
- Microsoft Visual Studio .NET 2003 Enterprise Architect
- Microsoft Visual Studio .NET 2003 Enterprise Developer
- Microsoft Visual Studio .NET 2003 Professional Edition
- Microsoft ASP.NET 1.1
| kbinfo kbvalidation kbhttpruntime KB837000 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate